我正在尝试编写触发某些内容的乌龟代码并关闭乌龟窗口,所以我尝试使用turtle.bye()
但我一直收到错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python36\lib\tkinter\__init__.py", line 1699, in
__call__
return self.func(*args)
File "C:\Program Files\Python36\lib\turtle.py", line 686, in eventfun
fun()
File "E:\Home made game\Chapter 1 Log Cabin.py", line 346, in k1
player.bye()
AttributeError: 'Turtle' object has no attribute 'bye'
答案 0 :(得分:0)
bye()
是Screen单例实例的方法,而不是Turtle。它还映射到turtle包中的顶级函数。它不适用于Turtle实例。您可以通过以下几种方式调用它:
import turtle
turtle.Screen().bye() # not a turtle instance, the turtle package
turtle.bye() # not a turtle instance, the turtle package
turtle.getscreen().bye() # not a turtle instance, the turtle package
yertle = turtle.Turtle()
yertle.getscreen().bye() # turtle instance gets screen singleton to invoke bye()
一旦你打电话给bye()
,乌龟世界将以一种不打算重新启动的方式关闭。