【问题标题】:Python turtle throws an error on closePython海龟在关闭时抛出错误
【发布时间】:2013-08-12 23:48:03
【问题描述】:

我有一个用 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"

我做错了什么? 我是 python 新手,所以任何帮助将不胜感激:)

谢谢

【问题讨论】:

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

标签: python tkinter turtle-graphics


【解决方案1】:

似乎您的循环在 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()

【讨论】:

    【解决方案2】:

    试试这个

    from turtle import *
    
    tim = Turtle()
    
    my_screen = Screen()
    
    #my_screen.exitonclick()
    
    tim.forward(100)
    my_screen.exitonclick()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多