简体   繁体   English

turtle.bye()后重新打开龟

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

I have some code as follows: 我有一些代码如下:

# My code here

turtle.bye()

After that, is there any way I can reopen the turtle window. 在那之后,有什么方法可以重新打开龟窗。 I know you can do turtle.clearscreen() but that does not close the turtle window. 我知道你可以做turtle.clearscreen()但不能关闭龟窗。

I will accept any answer which allows me to close the turtle graphics window and then reopen it without opening and running another python program to do this. 我会接受任何答案,它允许我关闭龟图形窗口,然后重新打开它,而无需打开和运行另一个python程序来执行此操作。

Thank you in advance 先感谢您

I've seen situations where the approach of @LukeTimmons worked but not always reliably and not in every situation. 我已经看到了@LukeTimmons的方法有效但不总是可靠的情况,而不是在所有情况下。 Give this solution a try: 试试这个解决方案:

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()

It resets two flags that keep turtle from starting up again. 它重置了两个标志,以防止龟再次启动。 It may be safer to create your own turtle after restart rather than use the default turtle which may point back to the departed environment. 在重新启动后创建自己的乌龟可能更安全,而不是使用可能指向离开环境的默认乌龟。

There may be other ways but this is the only way I know. 可能还有其他方法,但这是我所知道的唯一方式。

from turtle import *

def turtle1():
    #Your code here

turtle1()

turtle.bye()

turtle1()

This should re-run your code without re-typing it. 这应该重新运行您的代码而无需重新键入它。

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

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