Creating GUIs in Python using Tkinter: Build a Login App in 30 Minutes

Posted by


In this tutorial, we will learn how to build a Login App GUI using Python’s Tkinter library. Tkinter is the standard GUI toolkit that comes with Python and is easy to use for creating interactive graphical user interfaces.

We will create a simple login form with username and password fields, along with a submit button to authenticate the user. Let’s get started!

Step 1: Install Tkinter

Tkinter is included with Python, so you don’t need to install anything extra. If you are using Python 2.x, you will need to install Tkinter separately with the following command:

pip install tk

Step 2: Import the necessary libraries

We will start by importing the Tkinter module along with the messagebox module for displaying messages to the user.

import tkinter as tk
from tkinter import messagebox

Step 3: Create the main window

Next, we will create the main window for our login app using the Tk class. We will set the title, size, and background color of the window.

root = tk.Tk()
root.title("Login App")
root.geometry("300x200")
root.configure(bg="lightblue")

Step 4: Create username and password fields

Now, let’s create the username and password input fields using the Entry class. We will also add labels to identify each field.

username_label = tk.Label(root, text="Username", bg="lightblue")
username_label.pack()
username_entry = tk.Entry(root)
username_entry.pack()

password_label = tk.Label(root, text="Password", bg="lightblue")
password_label.pack()
password_entry = tk.Entry(root, show='*')
password_entry.pack()

Step 5: Create the submit button

We will create a submit button that will call a function to validate the username and password entered by the user.

def login():
    username = username_entry.get()
    password = password_entry.get()

    if username == "admin" and password == "admin123":
        messagebox.showinfo("Login Successful", "Welcome, Admin!")
    else:
        messagebox.showerror("Login Failed", "Invalid username or password")

submit_btn = tk.Button(root, text="Submit", command=login)
submit_btn.pack()

Step 6: Run the main loop

Finally, we will run the main loop to display the login app window and wait for user input.

root.mainloop()

And that’s it! You have successfully created a Login App GUI using Python’s Tkinter library in just 30 minutes. Feel free to customize the design and functionality according to your requirements.

I hope you found this tutorial helpful. Happy coding!

0 0 votes
Article Rating

Leave a Reply

44 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@AnilKumar-l2r4k
2 hours ago

Thank you so much. Best explanation. Kudos to you sir 😊

@BigBoss-er6pm
2 hours ago

bahut pasand aaya maza aagaya

@vihhuuu0099
2 hours ago

SIR, ACTUALLY ITS SAYING WRONG KEYWORD " FILE " ITRIED TO FIX IT WITH YOUR PROBLEM BUT NO SOLUTION PLEASE HELP

4TH GRADE STUDENT STRUGLLING FROM ERRORS

@YASIRKHAN-n3j
2 hours ago

nice teacher

@vijayprajapatiIITian
2 hours ago

sir , how to access a logo file from file explorer to vs code , please reply

@kapilparish649
2 hours ago

Good work

@CoderMala
2 hours ago

Sir please help me

@ayushanand568
2 hours ago

12:00

@SehriRana-r6u
2 hours ago

Just amazing! Great work sir, very helpful for me Thank sir very much:)

@hadisheraz2015
2 hours ago

no pip install. pillow

@deepeshjaiswal786
2 hours ago

Thanks sir

@MuhammadRamzan-yb9lz
2 hours ago

sir you solve my problem I don't have words to say thanx

@maithilivyas5948
2 hours ago

"favicon.ico" jaisi file kaise banaye

@maithilivyas5948
2 hours ago

how to create image file in python

@arnorajsarkar8669
2 hours ago

Unable to resize the image… it's appearing "Module img has no attribute named resize"

@GauriShankar-x8r
2 hours ago

Actually I am facing problem while adding the images and icon on idle and I can't install pycharm…..pls help me it's my school project and I ahve to submit this on day after tomorrow pls help

@GauriShankar-x8r
2 hours ago

Can we make this on idle python instead of pycharm

@mehediazad1780
2 hours ago

root = Tk()

in this line it gives me error saying 'TclError: couldn't connect to display ":5.0"'

@akashtribhuvan8124
2 hours ago

Thankyou Sir!

@rits3010
2 hours ago

I wish i could use front end technologies(html/css) for GUI in tkinter.

44
0
Would love your thoughts, please comment.x
()
x