函数中未设置标签变量

2024-10-01 00:20:29 发布

您现在位置:Python中文网/ 问答频道 /正文

为什么当我单击“登录”按钮时,标签会立即获得“未验证”值,并且不会显示“正在录制”和“已结束”。当我注释self.messageBoxText.set('Not Verified')时,标签显示“结束”,而不显示“录制”。代码如下:

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()

Tags: textselfmasterinitdefbuttonwidthframe
1条回答
网友
1楼 · 发布于 2024-10-01 00:20:29

GUI不会在您更改值时直接重新绘制小部件,而是在您退出函数时。退出函数后,它可能有更多的元素要重绘,它可以在一瞬间重绘所有元素,这样它使用的时间更少,而且不会闪烁。你知道吗

您可以使用self.update()强制GUI重新绘制小部件,time.sleep()在更改标签中的文本并使用另一个update()之前等待

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')

但是当您使用这个方法时,它会阻塞检查键/鼠标事件的mainloop,您可能会看到其他元素被冻结。你知道吗

要解决冻结小部件的问题,可以使用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')

但这种方法似乎很奇怪。你知道吗

相关问题 更多 >