Create Login Form Using Tkinter in Python: Simple Guide
To create a login form using
tkinter in Python, you build a window with Entry widgets for username and password, and a Button to submit. Use tkinter methods to get input and validate credentials when the button is clicked.Syntax
The basic syntax to create a login form in tkinter involves creating a Tk() window, adding Label widgets for text, Entry widgets for user input, and a Button to trigger login validation.
Tk(): Creates the main window.Label: Displays text like 'Username' or 'Password'.Entry: Input field for user to type.Button: Clickable button to submit the form.get(): Method to read text fromEntry.
python
import tkinter as tk root = tk.Tk() root.title('Login Form') # Username label and entry username_label = tk.Label(root, text='Username') username_label.pack() username_entry = tk.Entry(root) username_entry.pack() # Password label and entry password_label = tk.Label(root, text='Password') password_label.pack() password_entry = tk.Entry(root, show='*') password_entry.pack() # Login button def login(): user = username_entry.get() pwd = password_entry.get() print(f'Username: {user}, Password: {pwd}') login_button = tk.Button(root, text='Login', command=login) login_button.pack() root.mainloop()
Output
A window appears with Username and Password fields and a Login button. When clicked, it prints entered username and password in the console.
Example
This example shows a complete login form that checks if the username and password match preset values and shows a message accordingly.
python
import tkinter as tk from tkinter import messagebox root = tk.Tk() root.title('Login Form') root.geometry('300x150') # Labels and entries username_label = tk.Label(root, text='Username') username_label.pack(pady=(10,0)) username_entry = tk.Entry(root) username_entry.pack() password_label = tk.Label(root, text='Password') password_label.pack(pady=(10,0)) password_entry = tk.Entry(root, show='*') password_entry.pack() # Login function def login(): user = username_entry.get() pwd = password_entry.get() if user == 'admin' and pwd == '1234': messagebox.showinfo('Login Status', 'Login Successful!') else: messagebox.showerror('Login Status', 'Invalid username or password') login_button = tk.Button(root, text='Login', command=login) login_button.pack(pady=10) root.mainloop()
Output
A window with username and password fields and a Login button. If correct credentials ('admin' and '1234') are entered, a popup says 'Login Successful!'; otherwise, 'Invalid username or password'.
Common Pitfalls
Common mistakes when creating a login form with tkinter include:
- Not using
show='*'for password entry, which shows the password in plain text. - Forgetting to call
get()onEntrywidgets to retrieve input. - Not handling empty input fields before validation.
- Not using
mainloop()to start the GUI event loop.
Always validate inputs and provide user feedback with message boxes.
python
import tkinter as tk from tkinter import messagebox root = tk.Tk() root.title('Login Form') username_entry = tk.Entry(root) username_entry.pack() password_entry = tk.Entry(root, show='*') # Added show='*' to hide password password_entry.pack() # Wrong: forgetting to get input values # def login(): # if username_entry == 'admin' and password_entry == '1234': # messagebox.showinfo('Login', 'Success') # Right way: def login(): user = username_entry.get() pwd = password_entry.get() if not user or not pwd: messagebox.showwarning('Input Error', 'Please enter both username and password') return if user == 'admin' and pwd == '1234': messagebox.showinfo('Login', 'Success') else: messagebox.showerror('Login', 'Invalid credentials') login_button = tk.Button(root, text='Login', command=login) login_button.pack() root.mainloop()
Output
A window with username and password fields and a Login button. Password is hidden. If fields are empty and login clicked, a warning popup appears.
Quick Reference
Summary tips for creating a login form with tkinter:
- Use
Entry(show='*')for password fields to hide input. - Use
get()method to read user input fromEntry. - Use
messageboxto show feedback messages. - Always call
mainloop()to run the GUI. - Validate inputs before processing login.
Key Takeaways
Use tkinter's Entry widget with show='*' to hide passwords.
Retrieve user input with the get() method before validation.
Provide clear feedback using message boxes for success or errors.
Always start the GUI event loop with mainloop() to display the window.
Validate inputs to avoid empty username or password submissions.