有没有办法将Python Turtle剧本嵌入烧瓶中?

发布于 2025-01-23 05:02:20 字数 986 浏览 2 评论 0原文

我正在用烧瓶框架作为一个简单的项目制作网站,我想知道是否有一种嵌入Python Turtle脚本的方法,我知道我可以使用Jinja {%Python对于简单的python语句,supped%} {%结束python stupt%},但我不知道如何运行脚本。

要更具体地说,我想运行我制作的Python Turtle游戏,因此我需要以某种方式嵌入乌龟窗口,并将其“聆听”以获取用户输入,直到按某些键,例如ESC或P键。

说我想嵌入这个简单的乌龟游戏:

import turtle

#screen
wn = turtle.Screen()
wn.setup(width = 600,height = 600)
wn.title("Turtle Game")

#player
player = turtle.Turtle()
player.shape("turtle")
player.color("green")
player.up()
player.speed(0)
player.setposition(0,0)

#controls
def go_forw():
    player.forward(10)

def go_back():
    player.forward(-10)

def turn_r():
    player.right(5)

def turn_l():
    player.left(5)

#key input & loop
turtle.listen()
turtle.onkey(go_forw,'Up')
turtle.onkey(go_back,'Down')
turtle.onkey(turn_l,'Left')
turtle.onkey(turn_r,'Right')

turtle.mainloop()

我知道可以使用Replit完成,但是Replit不支持我想使用的某些模块,是否可以嵌入像这样的乌龟脚本不在网上托管您的代码?

我会为任何答案感到高兴。

提前致谢。

I am making a website with flask framework as a simple project, and I'm wondering if there is a way to embed a python turtle script, I know I can use the jinja {% python stuff %}{% end python stuff %} for simple python statements but I don't know how to run a script.

To be even more specific, I want to run a python turtle game I made, so I need to somehow embed the turtle window and make it "listen" for user input until you press certain keys, like the esc or p key for example.

Say I want to embed this simple turtle game:

import turtle

#screen
wn = turtle.Screen()
wn.setup(width = 600,height = 600)
wn.title("Turtle Game")

#player
player = turtle.Turtle()
player.shape("turtle")
player.color("green")
player.up()
player.speed(0)
player.setposition(0,0)

#controls
def go_forw():
    player.forward(10)

def go_back():
    player.forward(-10)

def turn_r():
    player.right(5)

def turn_l():
    player.left(5)

#key input & loop
turtle.listen()
turtle.onkey(go_forw,'Up')
turtle.onkey(go_back,'Down')
turtle.onkey(turn_l,'Left')
turtle.onkey(turn_r,'Right')

turtle.mainloop()

I know it can be done using replit, but replit doesn't support some modules I want to use, is it possible to embed a turtle script like this one without hosting your code somewhere online?

I will be glad for any answer.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

氛圍 2025-01-30 05:02:20

不幸的是,您无法通过烧瓶向用户展示Turtle GUI,尽管您可以将画布导出为PNG,然后通过烧瓶托管PNG。

You unfortunately cannot show the turtle GUI to users via flask, although you could export the canvas as a PNG, and then host the PNG via flask.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文