tkinter从根窗口暂停或停止一个函数(def)

时间:2015-02-26 14:06:08

标签: python tkinter window quit halt

如果从根窗口调用函数并且函数没有解决方案或者用户想要停止该函数,可以从根窗口完成,如果是,如何?以下代码生成两个按钮 - Start starts" while"使用start()但是Quit不能停止start()。在start()中使用root.update_idletasks()不会产生任何影响。

#!/usr/bin/env python
from Tkinter import *

def start():
    while True:
      print "Stop me if you can from Quit"
      root.update_idletasks()

root = Tk()
root.title('Example')

button1 = Button(root,text = 'Start', command = start)
button1.grid(row = 0, column = 0)

button2 = Button(root,text = 'Quit', command = root.destroy)
button2.grid(row = 1, column = 0)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

使用root.update_idletasks()root.update()替换start()从button2杀死根窗口。

#!/usr/bin/env python
from Tkinter import *

def start():
    while True:
      print "Stop me if you can from Quit"
      root.update()

root = Tk()
root.title('Example')

button1 = Button(root,text = 'Start', command = start)
button1.grid(row = 0, column = 0)

button2 = Button(root,text = 'Quit', command = root.destroy)
button2.grid(row = 1, column = 0)

root.mainloop()