I designed an arc which can rotate, but I can't figure out why IDLE tells me there is still an error in my code.
Here is my code:
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()
And this is the 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"
答案 0 :(得分: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()
答案 1 :(得分:0)
It seems to me that your code runs fine while it's running -- it's just ending it that is the problem. Did it give that error when you closed the application?
I modified the <h1>Laravel</h1>
Hello, @{{ name }}.
block to while True:
to verify this and as long as your loop doesn't expect to continue it closes perfectly.
Here are two possible ways you could fix this:
The easy way, catch the error:
for i in range(500):
The hard way, don't make the error happen (give the gui a second to catch up):
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()
Personally though, while I don't usually suggest catching errors, I wouldn't go with the second option. Essentially what I'm doing there is creating a variable that tells the while loop whether it should continue or not, then when you click the close button updating that while to end. The infinite while loop is the main problem here in my opinion. Once it ends and the GUI can finish, it all works out.