不知道为什么标签没有使用 tkinter 更新

Don't know why label is not updating using tkinter

我是 python 的初学者,正在尝试为学校课程制作一个简单的登录系统及其 GUI。

我做得很好,因为我是 python(和一般编码)的初学者,我正在尝试学习使用 tkinter。

除了当我试图让程序在用户“access = True”时更改标签文本时,我已经想通了一切。

这是代码片段:

from tkinter import *
from time import sleep

usernamelist = ["bob123","tim321","me","duda"]
passwordlist = ["banana","apple","password123","duda2000"]


def checklogin():
    global access
    global mode
    username = entuser.get()
    password = entpass.get()
    userfound = False
    if username == "admin":
        if password == "allowmein":
            access = True
            mode = "admin"
        else:
            lberror.config(text="Incorrect password, try again")
            entpass.delete(0, END)
    else:
        for i in range(len(usernamelist)):
            if username == usernamelist[i]:
                userfound = True
                if password == passwordlist[i]:
                    access = True
                else:
                    lberror.config(text="Incorrect password, try again")
                    entpass.delete(0, END)
    if userfound == False and username != "admin":
        lberror.config(text="Username not found, try again")
        entuser.delete(0, END)
        entpass.delete(0, END)
    if access == True:
        lberror.config(text= "Access Granted")
        sleep(1)
        mainlog.destroy()
        return access
        return mode


access = False
mode = "student"

mainlog = Tk()
mainlog.title("Maths Quiz Login")
lbuser = Label(mainlog, text= "Username: ")
lbpass = Label(mainlog, text= "Password: ")
entuser = Entry(mainlog,)
entpass = Entry(mainlog, show="*")
logbtn = Button(mainlog, text= "Login", command= checklogin)
lberror = Label(mainlog, text= "")

lbuser.grid(row=0, column=0)
lbpass.grid(row=1, column=0)
entuser.grid(row=0, column=1)
entpass.grid(row=1, column=1)
logbtn.grid(row=2, column=1)
lberror.grid(row=3,column = 0, columnspan = 2)

mainlog.geometry("250x150+100+100")

mainlog.mainloop()

当我尝试 运行 代码时,lberror 标签在命令时似乎服从所有命令,以显示不正确的密码或在相应事件发生时未找到用户名,但它无法显示已授予访问权限,我试图寻找解释,但找不到。

将标签更改为 "Access Granted" 后,您似乎正在破坏 window:mainlog.destroy()

如果删除销毁 window,则会显示 "Access Granted"。

如果确实希望window在登录成功后销毁,在sleep(1)前添加mainlog.update()

if access == True:
        lberror.config(text= "Access Granted")
        mainlog.update()
        sleep(1)
        mainlog.destroy()
        return access
        return mode