不知道为什么标签不使用tkin更新

2024-10-04 15:27:58 发布

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

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


Tags: textconfigtrueifaccessmodeusernamecolumn
1条回答
网友
1楼 · 发布于 2024-10-04 15:27:58

在将标签更改为“Access Granted”之后,似乎正在破坏窗口:mainlog.destroy()

如果删除该窗口,将显示“已授予访问权限”

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

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

相关问题 更多 >

    热门问题