退出在无限循环上运行的Tkinter应用程序

时间:2014-07-06 11:06:36

标签: python-3.x tkinter

我的申请并没有彻底放弃。错误消息是: tkinter.TclError:无效的命令名称“.47654392”

我的代码是:

import math
from tkinter import *
import time
tk=Tk()
c=Canvas(tk,width=800,height=600)
c.pack()
c.create_arc(200,200,250,250,extent=359,style=ARC)
def out():
    tk.after(5,lambda:tk.destroy())
b=Button(tk,text='Quit',command=out)
b.pack(side=BOTTOM)
a=2
theta=math.radians(0)
while a==2:
    for i in range(360):
        siny=math.sin(theta)*1
        cosx=math.cos(theta)*1
        c.move(1,cosx,siny)
        theta=theta+math.radians(1)
        tk.update()
        time.sleep(0.01)
tk.mainloop()

1 个答案:

答案 0 :(得分:0)

使用无限循环while您没有运行tk.mainloop。所以tkinter可以执行一些功能。

不要使用time.sleep(),因为您停止tkinter中的所有功能 - 使用after()

from tkinter import *
import math

master = Tk()

c = Canvas(master, width=800, height=600)
c.pack()

c.create_arc(200, 200, 250, 250, extent=359, style=ARC)

b = Button(master, text='Quit', command=master.destroy)
b.pack(side=BOTTOM)

theta = math.radians(0)
rad = math.radians(1)

def move():
    global theta
    siny = math.sin(theta)
    cosx = math.cos(theta)
    c.move(1, cosx, siny)
    theta += rad
    master.update()
    master.after(1, move)

master.after(1, move)

master.mainloop()
相关问题