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