简体   繁体   中英

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. 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(...)
  • 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM