🧑💻 How to Create Login Form in Python Tkinter (Full Code Explained)
If you’re learning Python GUI (Graphical User Interface) development, then Tkinter is the best and easiest toolkit to begin with. In this tutorial, we’ll learn how to create a simple yet functional Login Form in Python using Tkinter.
This is perfect for coding learners, beginners, or anyone who wants to build small desktop applications using Python. We’ll go step-by-step — designing the layout, adding input fields, and verifying user credentials.
🔍 What is Tkinter?
Tkinter is Python’s built-in library for creating desktop GUI applications. It allows you to design windows, buttons, labels, entry boxes, and many other interface components easily.
You don’t need to install anything extra — Tkinter comes pre-installed with Python.
To check if Tkinter is installed, open your Python shell and type:
import tkinter
print("Tkinter is working fine!")
If you don’t see any error, you’re ready to go! 🎯 Goal of This Tutorial
By the end of this tutorial, you’ll be able to:
✅ Create a Tkinter window
✅ Add Labels, Entry Boxes, and Buttons
✅ Validate username and password
✅ Show success or error messages
✅ Understand how Tkinter layout works
Let’s begin step-by-step.
🧩 Step 1: Import Tkinter and Create the Main Window
Every Tkinter app starts with importing the library and creating a main window (called root).
from tkinter import *
from tkinter import messagebox # for showing pop-up messages
# Create main window
root = Tk()
root.title("Login Form")
root.geometry("400x300") # width x height
root.config(bg="#f2f2f2") # background color
✅ Explanation:
- Tk() creates the main window.
- title() gives it a name.
- geometry() defines the window size.
- config(bg="color") sets the background color.
🧱 Step 2: Create Labels and Entry Widgets
Next, we’ll add text labels for username and password and create entry boxes for user input.
# Heading
Label(root, text="Login Form", font=("Arial", 18, "bold"), bg="#f2f2f2", fg="#333").pack(pady=10)
# Username label and entry
Label(root, text="Username:", font=("Arial", 12), bg="#f2f2f2").pack(pady=5)
username_entry = Entry(root, width=30, bd=2)
username_entry.pack(pady=5)
# Password label and entry
Label(root, text="Password:", font=("Arial", 12), bg="#f2f2f2").pack(pady=5)
password_entry = Entry(root, width=30, bd=2, show="*")
password_entry.pack(pady=5)
✅ Explanation: - Label() creates static text on the window.
- Entry() is used for input fields.
- show="*" hides the password characters (like in real login forms).
- .pack() is a geometry manager that arranges widgets vertically.
🧠 Step 3: Add Login Functionality
We need to write a function that checks if the username and password are correct.
For simplicity, let’s assume:
Username = admin
Password = 12345
def login():
username = username_entry.get()
password = password_entry.get()
if username == "admin" and password == "12345":
messagebox.showinfo("Login Success", "Welcome, admin!")
elif username == "" or password == "":
messagebox.showwarning("Input Error", "Please fill out both fields.")
else:
messagebox.showerror("Login Failed", "Invalid username or password!")
✅ Explanation:
- .get() retrieves the text from the entry boxes.
- We use if conditions to validate the input.
- messagebox shows popup messages with different icons.
⚙️ Step 4: Add the Login Button
We’ll now add a Login button that triggers the login() function when clicked.
# Login button
Button(root, text="Login", font=("Arial", 12, "bold"), bg="#4CAF50", fg="white", width=15, command=login).pack(pady=20)
✅ Explanation:
- Button() creates a clickable button.
- command=login calls our login() function when the button is pressed.
- bg and fg define background and text color respectively.
💡 Step 5: Add Exit Button (Optional)
Let’s also add a simple Exit button to close the window.
Button(root, text="Exit", font=("Arial", 12, "bold"), bg="#f44336", fg="white", width=15, command=root.destroy).pack()
✅ Explanation:
root.destroy closes the Tkinter window. 🏁 Step 6: Run the Application
Finally, we need to run our Tkinter event loop using:
root.mainloop()
This line keeps your window open and listens for user actions. 🧩 Full Source Code
Here’s the complete code of our Login Form:
from tkinter import *
from tkinter import messagebox
# Create main window
root = Tk()
root.title("Login Form")
root.geometry("400x300")
root.config(bg="#f2f2f2")
# Heading
Label(root, text="Login Form", font=("Arial", 18, "bold"), bg="#f2f2f2", fg="#333").pack(pady=10)
# Username
Label(root, text="Username:", font=("Arial", 12), bg="#f2f2f2").pack(pady=5)
username_entry = Entry(root, width=30, bd=2)
username_entry.pack(pady=5)
# Password
Label(root, text="Password:", font=("Arial", 12), bg="#f2f2f2").pack(pady=5)
password_entry = Entry(root, width=30, bd=2, show="*")
password_entry.pack(pady=5)
# Login function
def login():
username = username_entry.get()
password = password_entry.get()
if username == "admin" and password == "12345":
messagebox.showinfo("Login Success", "Welcome, admin!")
elif username == "" or password == "":
messagebox.showwarning("Input Error", "Please fill out both fields.")
else:
messagebox.showerror("Login Failed", "Invalid username or password!")
# Buttons
Button(root, text="Login", font=("Arial", 12, "bold"), bg="#4CAF50", fg="white", width=15, command=login).pack(pady=20)
Button(root, text="Exit", font=("Arial", 12, "bold"), bg="#f44336", fg="white", width=15, command=root.destroy).pack()
root.mainloop()
🧑🏫 Output
When you run this program:
- A Login Window will appear.
- You can type username and password.
- If both match (admin, 12345), it will show a “Login Success” message.
- If wrong, it shows “Login Failed”.
- If fields are empty, it warns you.
💬 Bonus: How to Improve This Login Form
Once you understand the basics, you can enhance this project further:
- Connect with a database (SQLite or MySQL) for real user data.
- Add Sign Up and Forgot Password buttons.
- Use CustomTkinter for a more modern UI.
- Validate input formats (like email or password strength).
- Save session info using Python variables.
❓ FAQ (Frequently Asked Questions)
Q1. Can I use Tkinter without internet?
👉 Yes, Tkinter is a built-in library, no internet needed.
Q2. Can I change the background color of the whole form?
👉 Yes! Use root.config(bg="lightblue") to set any color.
Q3. How do I hide the password text?
👉 Use show="*" inside the password Entry widget.
Q4. How can I make the window unresizable?
👉 Add root.resizable(False, False) to lock window size.
Q5. Can I add images or logos to my login form?
👉 Absolutely! You can use PhotoImage in Tkinter to load images.
Five Beautiful Login Form Design To get Source Code Free Join Our Telegram Group
🚀 Conclusion
Creating a Login Form in Python Tkinter is one of the best beginner projects to start learning GUI development.
You’ve now learned how to design windows, take input from users, and handle validation — the foundation for real-world desktop apps!
Start experimenting — change colors, fonts, or connect it with a real database.
Soon, you’ll be building full-fledged desktop apps using Python and Tkinter! 💪

.png)
.png)
.png)
.png)
.png)
.png)