Python 3.x 如何实施";“处理请求”-TkInter中的消息
我正在尝试实现一个弹出窗口,当操作在后台进行时,它应该显示在主窗口上。按下下面代码中的按钮将导致GUI冻结10秒而不显示任何消息,并最终使按钮变为绿色。冻结是正常的,但我想在10秒内显示弹出窗口。 任何帮助都将不胜感激!提前谢谢Python 3.x 如何实施";“处理请求”-TkInter中的消息,python-3.x,tkinter,Python 3.x,Tkinter,我正在尝试实现一个弹出窗口,当操作在后台进行时,它应该显示在主窗口上。按下下面代码中的按钮将导致GUI冻结10秒而不显示任何消息,并最终使按钮变为绿色。冻结是正常的,但我想在10秒内显示弹出窗口。 任何帮助都将不胜感激!提前谢谢 import tkinter as tk import time class GUI(tk.Tk): def __init__(self): super().__init__() self.button1 = tk.Butto
import tkinter as tk
import time
class GUI(tk.Tk):
def __init__(self):
super().__init__()
self.button1 = tk.Button(text="Start", command=self.make_green)
self.button1.pack()
def popup(self):
tl = tk.Toplevel(self)
tl.transient()
tk.Label(tl, text="Painting green").pack()
tl.grab_set()
return tl
def make_green(self):
wait_popup = self.popup()
time.sleep(10)
self.button1.config(bg="green")
wait_popup.destroy()
a = GUI()
a.mainloop()
您可以在代码中使用self.update()来显示弹出窗口
import tkinter as tk
import time
class GUI(tk.Tk):
def __init__(self):
super().__init__()
self.button1 = tk.Button(text="Start", command=self.make_green)
self.button1.pack()
def popup(self):
tl = tk.Toplevel(self)
tl.transient()
tk.Label(tl, text="Painting green").pack()
self.update()
tl.grab_set()
return tl
def make_green(self):
wait_popup = self.popup()
time.sleep(10)
self.button1.config(bg="green")
wait_popup.destroy()
a = GUI()
a.mainloop()
也可以使用线程
首先从线程导入线程
from threading import Thread
然后提出一种新的方法
def thread_it(self):
return Thread(target=self.make_green, daemon=True).start()
并更新按钮的命令
self.button1 = tk.Button(text="Start", command=self.thread_it)
您可以在代码中使用self.update()来显示弹出窗口
import tkinter as tk
import time
class GUI(tk.Tk):
def __init__(self):
super().__init__()
self.button1 = tk.Button(text="Start", command=self.make_green)
self.button1.pack()
def popup(self):
tl = tk.Toplevel(self)
tl.transient()
tk.Label(tl, text="Painting green").pack()
self.update()
tl.grab_set()
return tl
def make_green(self):
wait_popup = self.popup()
time.sleep(10)
self.button1.config(bg="green")
wait_popup.destroy()
a = GUI()
a.mainloop()
也可以使用线程
首先从线程导入线程
from threading import Thread
然后提出一种新的方法
def thread_it(self):
return Thread(target=self.make_green, daemon=True).start()
并更新按钮的命令
self.button1 = tk.Button(text="Start", command=self.thread_it)
非常感谢你!第二种线程方法解决了我的问题。非常感谢!第二种线程方法解决了我的问题。