Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python turtle在关闭时抛出错误_Python_Tkinter_Turtle Graphics - Fatal编程技术网

Python turtle在关闭时抛出错误

Python turtle在关闭时抛出错误,python,tkinter,turtle-graphics,Python,Tkinter,Turtle Graphics,我用Python2.7.5编写了这个简单的程序。基本上,我只是在屏幕上画一些随机的东西,但是当我关闭画布时,我得到一个奇怪的错误 import turtle import random import time turtle.hideturtle() class Mus: def __init__(self): turtle.setx(random.randint(1,100)) turtle.sety(random.randint(1,100))

我用Python
2.7.5
编写了这个简单的程序。基本上,我只是在屏幕上画一些随机的东西,但是当我关闭画布时,我得到一个奇怪的错误

import turtle
import random
import time

turtle.hideturtle()

class Mus:

    def __init__(self):
        turtle.setx(random.randint(1,100))
        turtle.sety(random.randint(1,100))
        turtle.circle(random.randint(1,100))

while True:
    Mus()

turtle.exitonclick()
当我关闭程序时,会出现以下错误:

Traceback (most recent call last):
  File "/Users/jurehotujec/Desktop/running.py", line 15, in <module>
    Mus()
  File "/Users/jurehotujec/Desktop/running.py", line 12, in __init__
    turtle.circle(random.randint(1,100))
  File "<string>", line 1, in circle
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py", line 1908, in circle
    self._rotate(w)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py", line 3110, in _rotate
    self._update()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py", line 2564, in _update
    self._update_data()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py", line 2555, in _update_data
    self._pencolor, self._pensize)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py", line 569, in _drawline
    self.cv.coords(lineitem, *cl)
  File "<string>", line 1, in coords
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2240, in coords
    self.tk.call((self._w, 'coords') + args)))
TclError: invalid command name ".4335016920"
回溯(最近一次呼叫最后一次):
文件“/Users/jurehotujec/Desktop/running.py”,第15行,在
Mus()
文件“/Users/jurehotujec/Desktop/running.py”,第12行,在__
海龟。圆圈(随机。随机数(1100))
文件“”,第1行,圆圈中
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py”,第1908行,圈中
自旋转(w)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib tk/turtle.py”,第3110行,在
自我更新()
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py”,第2564行,在更新中
自我更新数据()
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib tk/turtle.py”,第2555行,在更新数据中
自我.(铅笔色,自我.)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py”,第569行,在图中
自我简历协调(行项目,*cl)
文件“”,第1行,在坐标中
coords中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py”,第2240行
self.tk.call((self._w,'coords')+args)))
Tcl错误:无效的命令名“.4335016920”
我做错了什么? 我是python新手,因此非常感谢您的帮助:)


谢谢

似乎在Tk销毁了一些重要对象之后,您的循环试图继续。您可以通过标志向关闭事件发送信号:

import turtle
import random
import time
import Tkinter as tk



turtle.hideturtle()

closed = False
def on_close():
    global closed
    closed = True
    exit()

# Register hander for close event    
tk._default_root.protocol("WM_DELETE_WINDOW", on_close)


class Mus:

    def __init__(self):
        turtle.setx(random.randint(1,100))
        turtle.sety(random.randint(1,100))
        turtle.circle(random.randint(1,100))

# check the flag
while not closed:
    Mus()





turtle.exitonclick()
试试这个

from turtle import *

tim = Turtle()

my_screen = Screen()

#my_screen.exitonclick()

tim.forward(100)
my_screen.exitonclick()

我认为您不应该担心它-最终它将来自标准库内部的一些问题,这些问题围绕着退出过程中不适当的刷新事件。但是除了回溯,它正在做你想做的事情,对吗?是的,但它看起来很难看:)好吧,你可以
导入Tkinter
,然后在try/except块捕获中包装你的
Mus()
调用
Tkinter.TclError
,在except体中使用
break
,但这是个坏主意,因为它也会吞下“真实的”应用程序代码中的错误。但是这里有一个合法的bug,所以应该有一个回溯-只是你不应该做任何事情,这个bug与你的代码无关。