0

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

4

1 回答 1

0

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

如果您删除销毁窗口,则会显示“Access Granted”。

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

if access == True:
        lberror.config(text= "Access Granted")
        mainlog.update()
        sleep(1)
        mainlog.destroy()
        return access
        return mode
于 2018-05-03T07:09:06.120 回答