为什么当我点击登录按钮时,标签立即获得“未验证”值并且不显示“正在录制”和“已结束”。当我评论 self.messageBoxText.set('Not Verified') 时,标签显示“Ended”并且不显示“Recording”。代码如下:
import time
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.geometry('1250x400')
self.title('Speaker Verification System')
self._frame = None
self.switch_frame(StartPage)
def switch_frame(self, frame_class):
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.pack(fill='both')
class StartPage(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.messageBoxText = tk.StringVar()
self.messageBoxText.set('Click on "Sign In" button to enter.')
tk.Label(self, text="Locked", font=('courier new', 48, "bold")).pack(side="top", fill="x", pady=5)
tk.Label(self, textvariable=self.messageBoxText, font=('courier new', 24), borderwidth=2,
relief='groove', width=70, height=5).pack(pady=30)
tk.Button(self, text='Sign in', width=20, height=2, command=lambda: self.verify_login(master)).pack()
def verify_login(self, master):
verify = 2
self.messageBoxText.set('Recording...') # it doesn't set
self.messageBoxText.set('Ended...') # it doesn't set
if verify == 1:
self.messageBoxText.set('Login Successful')
master.switch_frame(PageOne)
return
self.messageBoxText.set('Not Verified')
class PageOne(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
tk.Frame.configure(self, width=1250, height=400)
self.monitorText = tk.StringVar()
self.monitorText.set('Welcome to the Speaker Verification System')
tk.Button(self, text='Linear SVC', command=self.btn_linear_svc_clicked).place(x=10, y=50, width=150, height=30)
tk.Button(self, text='Sign Out', command=lambda: master.switch_frame(StartPage)).place(x=10, y=100, width=150, height=30)
tk.Button(self, text='Quit', command=quit).place(x=10, y=150, width=150, height=30)
tk.Label(self, textvariable=self.monitorText, borderwidth=2, relief='groove').place(x=200, y=10, width=400, height=380)
def btn_linear_svc_clicked(self):
self.monitorText.set('Linear SVC Button Clicked...')
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
请您参考如下方法:
GUI 不会在您更改值时直接重绘小部件,而是在您退出函数时重绘。退出函数后,它可能有更多元素要重绘,它可以在一瞬间重绘所有元素,这样它使用的时间更少并且不会闪烁。
您可以使用 self.update() 强制 GUI 重绘小部件,并使用 time.sleep() 等待您更改标签中的文本并使用另一个 更新()
def verify_login(self, master):
verify = 2
self.messageBoxText.set('Recording...') # it doesn't set
self.update() # force GUI to redraw widgets
time.sleep(1)
self.messageBoxText.set('Ended...') # it doesn't set
self.update() # force GUI to redraw widgets
time.sleep(1)
if verify == 1:
self.messageBoxText.set('Login Successful')
master.switch_frame(PageOne)
return
self.messageBoxText.set('Not Verified')
但是当您使用此方法时,它会阻塞检查键/鼠标事件的主循环,您可能会看到其他元素被卡住。
要解决卡住小部件的问题,您可以使用 after(milliseconds, function_name) 要求 mainloop 延迟执行另一个函数。然后 mainloop 将有时间检查键/鼠标事件。
def verify_login(self, master):
verify = 2
self.messageBoxText.set('Recording...') # it doesn't set
self.after(1000, self.verify_2, verify) # 1000ms = 1s
def verify_2(self, verify):
self.messageBoxText.set('Ended...') # it doesn't set
self.after(1000, self.verify_3, verify) # 1000ms = 1s
def verify_3(self, verify):
if verify == 1:
self.messageBoxText.set('Login Successful')
master.switch_frame(PageOne)
return
self.messageBoxText.set('Not Verified')
但是这个方法可能看起来很奇怪。
