繁体   English   中英

turtle.bye()后重新打开龟

[英]Re-open turtle after turtle.bye()

我有一些代码如下:

# My code here

turtle.bye()

在那之后,有什么方法可以重新打开龟窗。 我知道你可以做turtle.clearscreen()但不能关闭龟窗。

我会接受任何答案,它允许我关闭龟图形窗口,然后重新打开它,而无需打开和运行另一个python程序来执行此操作。

先感谢您

我已经看到了@LukeTimmons的方法有效但不总是可靠的情况,而不是在所有情况下。 试试这个解决方案:

import time
import turtle

turtle.dot(200, 'green')

time.sleep(2)

turtle.bye()

# These two lines (indirectly) resurrect turtle environment after turtle.bye()
turtle.Turtle._screen = None  # force recreation of singleton Screen object
turtle.TurtleScreen._RUNNING = True  # only set upon TurtleScreen() definition

turtle.dot(200, 'red')

turtle.mainloop()

它重置了两个标志,以防止龟再次启动。 在重新启动后创建自己的乌龟可能更安全,而不是使用可能指向离开环境的默认乌龟。

可能还有其他方法,但这是我所知道的唯一方式。

from turtle import *

def turtle1():
    #Your code here

turtle1()

turtle.bye()

turtle1()

这应该重新运行您的代码而无需重新键入它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM