如何在满足条件后显示消息框?
How do I make the messagebox appear after the conditions are met?
我正在尝试通过 Tkinter 创建登录屏幕,我希望消息框在用户输入正确的用户名和密码或输入不正确时显示一条消息。
当我运行程序并输入用户名和密码时,消息框没有出现。有办法解决这个问题吗?
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()
您的代码没有 运行,因此我不得不更改一些内容。你也有它,所以你必须按下登录按钮才能显示用户名和密码框。这就是我所拥有的,我相信这就是你想要完成的。
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()
问题
您的代码中存在多个问题,主要围绕提供错误的变量名称。一些主要问题是:
- 在 loginbutton 中,您尝试访问
username_entry
和 password_entry
变量,但是在构建小部件时您做了 username = Entry(...)
和 password = Entry(...)
- 单击“登录”按钮时将调用的
command
定义为 login
,这与创建用户名和密码条目以及登录按钮的功能相同
解决方案
我相信你想要这样的东西:
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)
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()
重点是在创建登录按钮时,指定command
到checkLogin
,这是用户点击登录按钮后执行任何检查的功能。
我正在尝试通过 Tkinter 创建登录屏幕,我希望消息框在用户输入正确的用户名和密码或输入不正确时显示一条消息。 当我运行程序并输入用户名和密码时,消息框没有出现。有办法解决这个问题吗?
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()
您的代码没有 运行,因此我不得不更改一些内容。你也有它,所以你必须按下登录按钮才能显示用户名和密码框。这就是我所拥有的,我相信这就是你想要完成的。
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()
问题
您的代码中存在多个问题,主要围绕提供错误的变量名称。一些主要问题是:
- 在 loginbutton 中,您尝试访问
username_entry
和password_entry
变量,但是在构建小部件时您做了username = Entry(...)
和password = Entry(...)
- 单击“登录”按钮时将调用的
command
定义为login
,这与创建用户名和密码条目以及登录按钮的功能相同
解决方案
我相信你想要这样的东西:
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)
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()
重点是在创建登录按钮时,指定command
到checkLogin
,这是用户点击登录按钮后执行任何检查的功能。