海龟图形 - 如何控制窗口何时关闭?

发布于 2024-11-13 12:26:43 字数 330 浏览 7 评论 0原文

我有一个小的 python 脚本,可以绘制一些海龟图形。当我的脚本运行完毕后,海龟屏幕会自动关闭,因此为了能够看到图形一段时间,我必须在脚本末尾使用 time.sleep(5) 来延迟关闭。

有什么办法可以让它更加动态,即告诉 python 我想自己控制窗口的关闭?我不介意脚本在等待我的命令时是否不能执行任何其他操作,但我更希望不必转到控制台进行 read() 或其他操作。理想情况下,即使在脚本完成运行后,画布也应该保持打开状态,但我可以接受一个解决方案,该解决方案可以停止脚本,直到我关闭保存画布的窗口(或单击画布,或其他...)。

我该如何实现这个目标?

I have a small python script which draws some turtle graphics. When my script has finished running, the turtle screen automatically closes, so to be able to see the graphics for a while I have to use time.sleep(5) at the end of the script to delay the closing.

Is there any way I can make this more dynamic, i.e. tell python that I want to control the closing of the window myself? I don't mind if the script can't do anything else while waiting for my command, but I'd prefer if I didn't have to go to the console for a read() or something. Ideally, the canvas should stay open even after the script finishes running, but I am OK with a solution that halts the script until I close the window that holds the canvas (or click the canvas, or whatever...).

How do I accomplish this?

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

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

发布评论

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

评论(5

淡忘如思 2024-11-20 12:26:43

只需使用 turtle.done()turtle.Screen().exitonclick()作为海龟程序的最后一个命令。

Just use turtle.done() or turtle.Screen().exitonclick() as a last command of your turtle program.

清醇 2024-11-20 12:26:43
import turtle

turtle.forward(100)
turtle.left(90)
turtle.forward(100)
# etc.

turtle.getscreen()._root.mainloop()  # <-- run the Tkinter main loop

(编辑:turtle.done() 正如下面 hua 所建议的那样,没那么难看。)

import turtle

turtle.forward(100)
turtle.left(90)
turtle.forward(100)
# etc.

turtle.getscreen()._root.mainloop()  # <-- run the Tkinter main loop

(edit: turtle.done() as suggested by hua below is less ugly.)

╭ゆ眷念 2024-11-20 12:26:43

只需使用从turtle模块本身导入的mainloop()函数即可!

import turtle


#Draw a square
for i in range(4):
    turtle.forward(200)
    turtle.left(90)


#calling for the mainloop()
turtle.mainloop()

simply use the mainloop() function imported from turtle's module itself!.

import turtle


#Draw a square
for i in range(4):
    turtle.forward(200)
    turtle.left(90)


#calling for the mainloop()
turtle.mainloop()
难得心□动 2024-11-20 12:26:43

尝试在代码末尾添加 input()

Try adding input() at the end of your code.

南笙 2024-11-20 12:26:43

这将等待几次单击 - 并在您单击时绘制螺旋 - 直到它决定在最后一次单击时退出:

import turtle


win = turtle.Screen()
win.bgcolor("white")

tess = turtle.Turtle()

tess.speed(0)
tess.color("blue")             
tess.pensize(5)                 
offSet=30

def doNextEvent(x,y):

    global offSet
    global win
    tess.forward(20)
    tess.left(1+offSet)
    offSet=offSet-2
    if(offSet<1):
        win.exitonclick()


win.onclick(doNextEvent)
win.listen()
win.mainloop()

This waits for several clicks - and draws a spiral while you click - until it decides to exit on the last click:

import turtle


win = turtle.Screen()
win.bgcolor("white")

tess = turtle.Turtle()

tess.speed(0)
tess.color("blue")             
tess.pensize(5)                 
offSet=30

def doNextEvent(x,y):

    global offSet
    global win
    tess.forward(20)
    tess.left(1+offSet)
    offSet=offSet-2
    if(offSet<1):
        win.exitonclick()


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