0
当我按下“开始程序”按钮时,它会启动5秒的任务并阻止GUI。 据我所知,我需要使用线程,所以每个按钮将独立于GUI工作。 我已经被困了近一个月了,有人可以告诉我如何执行def start_Button(self)
:函数使用线程吗?当按下按钮时,Tkinter GUI卡住直到任务结束
from tkinter import *
import time
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.var = IntVar()
self.master.title("GUI")
self.pack(fill=BOTH, expand=1)
quitButton = Button(self, text="Exit", command=self.client_exit)
startButton = Button(self, text="Start Program", command=self.start_Button)
quitButton.grid(row=0,column=0)
startButton.grid(row=0, column=2)
def client_exit(self):
exit()
def start_Button(self):
print('Program is starting')
for i in range (5):
print(i)
time.sleep(1)
root = Tk()
root.geometry("200x50")
app = Window(root)
root.title("My Program")
root.mainloop()
我已经尝试了很长一段时间这样的解决方案,但无法将线程模块放置在正确的位置,现在,您告诉我这是有道理的!我知道使用线程是棘手的,并且可能会干扰,但在我的程序中(现在不是这种情况)。谢谢! –