Home » Tutorials » How to Make a Simple Login System using Python tkinter

How to Make a Simple Login System using Python tkinter

What we are going to do in today’s article is create a login window using the tkinter library which allows us to create a window where the user can enter a username and a password and then press login so that a message appears to indicate the success or failure of this endeavor.

Learn also: How to Set Up a User Authentication System with Flask

Table of Contents

First, to ensure the code functions properly, make sure to install the tkinter library using the terminal or your command prompt:

$ pip install tk 

Imports

from tkinter import *
from tkinter import messagebox

We start by importing the tkinter library, which allows the use of a graphical user interface (GUI), as well as importing the messagebox module from tkinter that permits the use of message boxes as the name suggests.

Login Function

def login():
    username = entry_username.get()
    password = entry_password.get()

    if username == "admin" and password == "admin":
        messagebox.showinfo("Login", "Login successful")
    else:
        messagebox.showerror("Login Error", "Incorrect username or password")

Next, we define a function that gets triggered once the user clicks the “login” button, and once triggered it retrieves the username and the password from their entry widgets, verifies them if they both are admin, and then a login successful message shows up. If not, the incorrect username or password shows up.

Creating the Main Window

# Create the main window
root = Tk()
root.title("Login - The Pycodes")
root.geometry("300x150")

Now, we create the main window, give it a title, and set its size.

Username Entry

# Username label and entry
label_username = Label(root, text="Username:")
label_username.pack()
entry_username = Entry(root)
entry_username.pack()

After that, we create a label that says “username” and below it an entry widget where the user can input the username.

Password Entry

# Password label and entry
label_password = Label(root, text="Password:")
label_password.pack()
entry_password = Entry(root, show="*") # Show asterisks for password input
entry_password.pack()

Here, we created a label that says “password” and an entry widget where the user can input the password and have it appear as *.

Login Button

# Login button
login_button = Button(root, text="Login", command=login)
login_button.pack()

Following that, we create a button that says “login“, once pressed it calls the login Function.

Main Event Loop

# Start the main event loop
root.mainloop()

Lastly, this keeps the main window in a loop so it can respond to the user’s actions and events.

Example

Full Code

from tkinter import *
from tkinter import messagebox


def login():
    username = entry_username.get()
    password = entry_password.get()


    if username == "admin" and password == "admin":
        messagebox.showinfo("Login", "Login successful")
    else:
        messagebox.showerror("Login Error", "Incorrect username or password")


# Create the main window
root = Tk()
root.title("Login - The Pycodes")
root.geometry("300x150")


# Username label and entry
label_username = Label(root, text="Username:")
label_username.pack()
entry_username = Entry(root)
entry_username.pack()


# Password label and entry
label_password = Label(root, text="Password:")
label_password.pack()
entry_password = Entry(root, show="*") # Show asterisks for password input
entry_password.pack()


# Login button
login_button = Button(root, text="Login", command=login)
login_button.pack()


# Start the main event loop
root.mainloop()

Happy Coding!

Subscribe for Top Free Python Tutorials!

Receive the best directly.  Elevate Your Coding Journey!

2 thoughts on “How to Make a Simple Login System using Python tkinter”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
×