简体   繁体   English

满足条件后如何让消息框出现?

[英]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()

Issue问题

There are multiple issues in your code, mainly revolving around giving the wrong variable names.您的代码中有多个问题,主要是围绕提供错误的变量名称。 Some of the main issues are:一些主要问题是:

  • In loginbutton you try to access username_entry and password_entry variable yet when building the widget you did username = Entry(...) and password = Entry(...)在 loginbutton 中,您尝试访问username_entrypassword_entry变量,但在构建小部件时,您执行了username = Entry(...)password = Entry(...)
  • the 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 button单击“登录”按钮时将调用的command被定义为login ,这与创建用户名和密码条目和登录按钮的 function 相同

Solution解决方案

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.

相关问题 如何在不同的语句中循环多个条件,如果条件不满足则使其循环? - How do I loop multiple conditions in different statements, and make it loop If the conditions are not met? 如何使程序循环直到满足条件? - How can I make it so that the program loops until the conditions are met? 如何使文本出现在 Matplotlib 中的 3barplot 之后 - How do I make the text appear after 3barplot in Matplotlib 如何通过 wxPython 在 messageBox 中使用 python 换行? - How do I make a newline with python in a messageBox through wxPython? 在满足条件之前,我该如何重复“ if”语句? - How do I make a repeating 'if' statement until a condition is met? 如果满足某些条件,如何在继续下一次迭代之前保持在 for 循环中? - How do I stay within a for loop if certain conditions are met, before continuing to the next iteration? Python,但似乎满足条件但无法识别语句 - Python if statement not recognized though conditions appear to be met 如何创建一个仅在满足某些条件时才进行实例化的类? - How do I create a class where instantiation only happens if certain conditions are met? 除非符合条件,否则如何在Django admin中显示删除确认 - How do I not show delete confirmation in Django admin unless conditions are met 当两个条件都满足时,如何使 Discord Bot 响应? - How to make a Discord Bot respond when two conditions are both met?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM