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

时间:2018-05-03 07:03:53

标签: python tkinter login logic labels

我是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标签在被命令显示错误的密码或在相应事件发生时找不到用户名时,似乎服从所有命令,但它无法显示授予的访问权限,我试图寻找解释,我无法找到。

1 个答案:

答案 0 :(得分:0)

将标签更改为“授予访问权限”后,您似乎正在销毁窗口:mainlog.destroy()

如果删除销毁窗口,则会显示“已授予访问权限”。

如果您确实希望在成功登录后销毁该窗口,请在mainlog.update()之前添加sleep(1)

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