通过 X 按钮关闭海龟窗口(关闭) [英] Close turtle window by X button(close)

查看:45
本文介绍了通过 X 按钮关闭海龟窗口(关闭)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 Python 代码,我正在学习 Turtle.当我通过 X 按钮关闭海龟窗口时,出现错误.我能做什么?

I'm coding Python and I'm learning Turtle. When I close turtle window by X button I'm getting an error. What can I do?

推荐答案

通常,通过按钮关闭窗口时产生的错误是由于使用 while True: 循环滥用了海龟的事件模块定时事件和其他方法.

Usually, errors generated upon closing the window via its buttons is due to abusing turtle's event module by using a while True: loop instead of timed events and other approaches.

如果您的程序是这种情况,请参阅此答案以获取有关如何设计代码的详细示例正确.

If this the case with your program, see this answer for a detailed example of how to go about designing your code properly.

我会避免任何涉及将代码包装在 try: 表达式中以捕获 Terminator 错误的解决方案,因为这是创可贴而不是正确的设计.考虑:

I would avoid any solutions that involve wrapping your code in a try: expression to catch the Terminator error as that's a band-aid and not a proper design. Consider:

from turtle import Screen, Turtle

def one_step():
    
    # do one iteration of useful stuff here

    screen.ontimer(one_step)


screen = Screen()

one_step()

screen.mainloop()

这篇关于通过 X 按钮关闭海龟窗口(关闭)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆