【发布时间】:2023-03-20 12:01:01
【问题描述】:
当我按下“启动程序”按钮时,它会启动一个 5 秒的任务并阻止 GUI。
据我了解,我需要使用线程,因此每个按钮都将独立于 GUI 工作。
我已经卡了快一个月了,有人可以告诉我如何执行def start_Button(self):使用线程的函数吗?
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()
【问题讨论】:
标签: python tkinter python-multithreading