0

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()
  • 1
    Which part of your code calls `loginbutton`? It is never called. Also don't name a variable the same as a function and it is `Label` not `label` (on one of your lines). – Matiiss Jan 02 '22 at 22:11
  • I'm guessing you're coming from a different language. In python you don't need the parantheses around `if`. – CozyCode Jan 02 '22 at 22:13
  • You have both a function and a variable named `loginbutton`, and you never call the function which is where the messagebox code is. – Bryan Oakley Jan 02 '22 at 22:17

2 Answers2

1

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

Lzypenguin
  • 945
  • 1
  • 7
  • 18
1

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)

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.

Tyzeron
  • 186
  • 2
  • you are indeed correct. Forgot that the widget's variables are being defined in the global scope. Edited answer. – Tyzeron Jan 02 '22 at 23:00