这是一个使用的简短示例threading
。我拿出你的other
功能,我不知道你为什么使用itertools
这里。我也将其取出并使用简单的线程示例进行简单设置。
一些东西:
您设置使用threading.Thread
作为基类Threader
,但你实际上从未已初始化基类。
每当你使用线程时,你通常想要定义一个run
方法然后使用start()
启动线程。呼唤start()
将会通知run
.
您需要使用线程来防止 GUI 阻塞,因为 tkinter 只是一个巨大循环上的一个线程。因此,每当您有一些长时间运行的进程时,它就会阻塞该线程,直到当前进程完成。这就是为什么它被放在另一个线程中。 Python 有一个东西叫做GIL https://wiki.python.org/moin/GlobalInterpreterLock,这可以防止true并行化(我编了这个词),因为一次只能使用一个线程。相反,它使用时间切片,GIL 在它们之间进行“轮询”以给出外貌多个任务同时运行。对于真正的并行处理,您应该使用multiprocessing
.
在下面的代码中我使用了self.daemon = True
。将线程设置为守护进程会在退出主程序时杀死它(在本例中为 Tk GUI)
from tkinter import *
import threading, time
class Threader(threading.Thread):
def __init__(self, *args, **kwargs):
threading.Thread.__init__(self, *args, **kwargs)
self.daemon = True
self.start()
def run(self):
while True:
print("Look a while true loop that doesn't block the GUI!")
print("Current Thread: %s" % self.name)
time.sleep(1)
if __name__ == '__main__':
root = Tk()
leftFrame = Frame(root)
leftFrame.pack(side=LEFT)
rightFrame = Frame(root)
rightFrame.pack(side=RIGHT)
playButton = Button(leftFrame, text="Play", fg="blue",
command= lambda: Threader(name='Play-Thread'))
stopButton = Button(rightFrame, text="Stop", fg="red",
command= lambda: Threader(name='Stop-Thread'))
playButton.pack(side=TOP)
stopButton.pack(side=BOTTOM)
root.mainloop()