您好我有一個小型的python gui界面,帶有兩個按鈕,啓動(啓動計數器)和停止(即設置停止計數器),計數器是一個無限循環,因爲我不希望它結束,除非點擊第二個按鈕。問題是第二個按鈕無法點擊,而第一個按鈕的功能仍在運行。 我讀過,我需要使用線程,我已經嘗試過,但我不完全瞭解如何做到這一點。請幫忙。Python,Tkinter:我如何防止使用線程tkinter gui mainloop崩潰
from Tkinter import *
import threading
class Threader(threading.Thread):
def run(self):
for _ in range(10):
print threading.current_thread().getName()
def main(self):
import itertools
for i in itertools.count(1, 1):
print i
def other(self):
print "Other"
m = Threader(name="main")
o = Threader(name="other")
try:
'''From here on we are building the Gui'''
root = Tk()
'''Lets build the GUI'''
'''We need two frames to help sort shit, a left and a right vertical frame'''
leftFrame = Frame(root)
leftFrame.pack(side=LEFT)
rightFrame = Frame(root)
rightFrame.pack(side=RIGHT)
'''Widgets'''
'''Buttons'''
playButton = Button(leftFrame, text="Play", fg="blue", command=m.main)
stopButton = Button(rightFrame, text="Stop", fg="red", command=o.other)
playButton.pack(side=TOP)
stopButton.pack(side=BOTTOM)
root.mainloop()
except Exception, e:
print e