[英]How do I make the messagebox appear after the conditions are met?
I am trying to create a login screen via Tkinter, and I want the messagebox to display a message when the user has entered the correct username and password, or hasn't entered it correctly.我正在尝试通过 Tkinter 创建登录屏幕,并且我希望消息框在用户输入正确的用户名和密码或未正确输入时显示消息。 When I run the program and enter the username and password, the messagebox does not appear.
当我运行程序并输入用户名和密码时,没有出现消息框。 Is there a way to fix this?
有没有办法来解决这个问题?
from tkinter import *
from tkinter import messagebox
root = Tk()
root.geometry("300x270")
root.title("staff login")
root.resizable(False, False)
root.configure(background ="black")
label = Label(root, text = "staff log-in")
frame_heading = Frame(root)
frame_heading.grid(row=0, column=0, columnspan=2, padx=25,pady=10)
frame_entry = Frame(root)
frame_entry.grid(row=1, column=0, columnspan=2, padx=25, pady=10)
Label(frame_heading,text ="Staff log-in",
font =('Arial',16))\
.grid(row=0, column=0, padx=0, pady=5)
Label(frame_entry, text = "Username: ")\
.grid(row=2, column=0, padx=10, pady=5)
label(frame_entry, text = "Password: ")\
.grid(row=3, column=0, padx=10, pady=5)
def login():
label.config (text = "log in")
username = Entry(frame_entry, width = 15, bg = "white")
username.grid(row=2, column=1, padx=5,pady=5)
password = Entry(frame_entry, width = 15, bg = "white")
password.grid(row=3, column=1, padx=5,pady=5)
loginbutton = Button(root, text="Login",width=7,
command=login)
loginbutton.grid(row=2, column=0, padx=0, pady=5)
def loginbutton():
username = username_entry.get()
password = password_entry.get()
if (username == 'admin' and password == 'abcd1234'):
messagebox.showinfo('info', 'Correct Login')
else:
messagebox.showinfo('info', 'Invalid Login')
root.mainloop()
Your code did not run, so I had to change a few things.你的代码没有运行,所以我不得不改变一些事情。 You also had it so that you had to press the login button to get the username and password boxes to appear.
您还拥有它,因此您必须按下登录按钮才能显示用户名和密码框。 Here is what I have that I believe is what you are trying to accomplish.
这就是我所拥有的,我相信这是你想要完成的。
from tkinter import *
from tkinter import messagebox
root = Tk()
root.geometry("300x270")
root.title("staff login")
root.resizable(False, False)
root.configure(background="black")
label = Label(root, text="staff log-in")
frame_heading = Frame(root)
frame_heading.grid(row=0, column=0, columnspan=2, padx=25, pady=10)
frame_entry = Frame(root)
frame_entry.grid(row=1, column=0, columnspan=2, padx=25, pady=10)
Label(frame_heading, text="Staff log-in",
font=('Arial', 16)).grid(row=0, column=0, padx=0, pady=5)
Label(frame_entry, text="Username: ").grid(row=2, column=0, padx=10, pady=5)
Label(frame_entry, text="Password: ").grid(row=3, column=0, padx=10, pady=5)
# def login(): -- I commented this line because the username and password entries should automatically populate
# -- The way you were doing it before these did not populate until after you pressed login
label.config(text="log in")
username = Entry(frame_entry, width=15, bg="white")
username.grid(row=2, column=1, padx=5, pady=5)
password = Entry(frame_entry, width=15, bg="white")
password.grid(row=3, column=1, padx=5, pady=5)
# Added the lambda command to the login function you wrote, which was never called anywhere else in the script.
loginbutton = Button(root, text="Login", width=7, command=lambda : loginbutton(username, password))
loginbutton.grid(row=2, column=0, padx=0, pady=5)
def loginbutton(username_entry, password_entry):
username = username_entry.get()
password = password_entry.get()
if (username == 'admin' and password == 'abcd1234'):
messagebox.showinfo('info', 'Correct Login')
else:
messagebox.showinfo('info', 'Invalid Login')
root.mainloop()
There are multiple issues in your code, mainly revolving around giving the wrong variable names.您的代码中有多个问题,主要是围绕提供错误的变量名称。 Some of the main issues are:
一些主要问题是:
username_entry
and password_entry
variable yet when building the widget you did username = Entry(...)
and password = Entry(...)
username_entry
和password_entry
变量,但在构建小部件时,您执行了username = Entry(...)
和password = Entry(...)
command
which is going to be invoked when the "Login" button is click is defined as login
which is the same function that creates the username & password Entry and the Login buttoncommand
被定义为login
,这与创建用户名和密码条目和登录按钮的 function 相同I believe you wanted something like this:我相信你想要这样的东西:
from tkinter import *
from tkinter import messagebox
def checkLogin():
username = username_entry.get()
password = password_entry.get()
if (username == 'admin' and password == 'abcd1234'):
messagebox.showinfo('info', 'Correct Login')
else:
messagebox.showinfo('info', 'Invalid Login')
root = Tk()
root.geometry("300x270")
root.title("staff login")
root.resizable(False, False)
root.configure(background ="black")
label = Label(root, text = "staff log-in")
frame_heading = Frame(root)
frame_heading.grid(row=0, column=0, columnspan=2, padx=25,pady=10)
frame_entry = Frame(root)
frame_entry.grid(row=1, column=0, columnspan=2, padx=25, pady=10)
Label(frame_heading,text ="Staff log-in",
font =('Arial',16))\
.grid(row=0, column=0, padx=0, pady=5)
Label(frame_entry, text = "Username: ")\
.grid(row=2, column=0, padx=10, pady=5)
Label(frame_entry, text = "Password: ")\
.grid(row=3, column=0, padx=10, pady=5)
global username_entry, password_entry
label.config (text = "log in")
username_entry = Entry(frame_entry, width = 15, bg = "white")
username_entry.grid(row=2, column=1, padx=5,pady=5)
password_entry = Entry(frame_entry, width = 15, bg = "white")
password_entry.grid(row=3, column=1, padx=5,pady=5)
loginbutton = Button(root, text="Login",width=7,
command=checkLogin)
loginbutton.grid(row=2, column=0, padx=0, pady=5)
root.mainloop()
The main point is that when creating the Login button, specify the command
to checkLogin
which is the function where you perform any checks after the user clicks the Login button.要点是,在创建登录按钮时,指定
checkLogin
command
,即 function 在用户单击登录按钮后执行任何检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.