Python Tkinter TclError

问题描述 投票:0回答:2

我设计了一个可以旋转的弧形,但我无法弄清楚为什么IDLE告诉我代码中仍然存在错误。

这是我的代码:

from Tkinter import*
from math import *
from time import sleep
pai=Tk()
cv=Canvas(pai,width=1100,height=631,bg="white")
cv.pack()
bb=(150,110,550,510)
temp1=0
temp2=24
t=0
arc1=cv.create_arc(bb,start=temp1,extent=temp2,fill="yellow")
while True:
    t=0.51
    temp1+=t
    cv.itemconfig(arc1,start=temp1)
    cv.update()

这是results

Traceback (most recent call last):
  File "C:\Users\amazi\Desktop\作业\s.py", line 15, in <module>
    cv.itemconfig(arc1,start=temp1)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2408, in itemconfigure
    return self._configure(('itemconfigure', tagOrId), cnf, kw)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1321, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
TclError: invalid command name ".93591304L"
python tkinter
2个回答
1
投票

我相信你得到了这个错误,因为你试图访问小部件,而没有,类似于this question。当您使用x按钮关闭应用程序时,while True循环尝试在不存在的小部件上至少再运行一次,从而产生错误。如果通过关闭命令提示符关闭应用程序,则不会产生错误,它应出现在何处?试试下面的例子,它可以完全按照你想要的方式设置动画,但是没有while True循环,无论如何都不容易在tkinter中使用:

import tkinter as tk
root = tk.Tk()
cv = tk.Canvas(root, width=1100, height=631, bg='white')
cv.pack()
bb = (150,110,550,510)
temp1 = 0
temp2 = 24
arc1=cv.create_arc(bb, start=temp1, extent=temp2, fill='yellow')

def rotate():
    global temp1
    t = 0.51
    temp1 = (temp1 + t) % 360
    print(temp1)
    cv.itemconfig(arc1,start=temp1)
    cv.update_idletasks()
    cv.after(0, rotate)

cv.after(0, rotate)
root.mainloop()

在下面使用Bryan的建议可能会产生另一个例子。您可能需要在下面的示例中调整t

import tkinter as tk
root = tk.Tk()
cv = tk.Canvas(root, width=1100, height=631, bg='white')
cv.pack()
bb = (150,110,550,510)
temp1 = 0
temp2 = 24
arc1=cv.create_arc(bb, start=temp1, extent=temp2, fill='yellow')

def rotate():
    global temp1
    t = 1
    temp1 = (temp1 + t) % 360
    print(temp1)
    cv.itemconfig(arc1,start=temp1)
    cv.after(1, rotate)

rotate()
root.mainloop()

0
投票

在我看来,你的代码在运行时运行良好 - 它只是结束它就是问题。关闭应用程序时是否出现错误?

我将while True:块修改为for i in range(500):以验证这一点,只要您的循环不期望继续它完全关闭。

您可以通过以下两种方法解决此问题:

简单的方法,捕捉错误:

from Tkinter import*

pai=Tk()
cv=Canvas(pai,width=1100,height=631,bg="white")
cv.pack()
bb=(150,110,550,510)
temp1=0
temp2=24
t=0
arc1=cv.create_arc(bb,start=temp1,extent=temp2,fill="yellow")
while True:
    t=0.51
    temp1+=t
    try:
        cv.itemconfig(arc1,start=temp1)
        cv.update()
    except TclError:
        pass
pai.mainloop()

困难的是,不要让错误发生(给gui一秒钟赶上):

import Tkinter as tk

def on_closing():
    pai.cont = False

pai = tk.Tk()
pai.protocol("WM_DELETE_WINDOW", on_closing)
cv = tk.Canvas(pai,width=1100,height=631,bg="white")
cv.pack()
bb = (150,110,550,510)
temp1 = 0
temp2 = 24
t = 0
arc1 = cv.create_arc(bb,start=temp1,extent=temp2,fill="yellow")

pai.cont = True
while pai.cont:
    t=0.51
    temp1+=t
    cv.itemconfig(arc1,start=temp1)
    cv.update()

pai.destroy()

就个人而言,虽然我通常不建议捕捉错误,但我不会选择第二种选择。基本上我正在做的是创建一个变量,告诉while循环是否应该继续,然后当你单击关闭按钮更新那个时间结束时。在我看来,无限循环是这里的主要问题。一旦它结束并且GUI可以完成,一切都会成功。

© www.soinside.com 2019 - 2024. All rights reserved.