Tkinter in Python
Tkinter in Python
If you want to create GUI (Graphical User Interface) applications in Python, Tkinter is the most popular and beginner-friendly library. It is built into Python, so you don't need to install anything separately. This article explains Tkinter with examples, widgets, layout managers, aand inbuilt functions, making it perfect for students, beginners, and exam aspirants.
What is Tkinter?
TKinter is Python's standard GUI package used to create desktop applications.
It provides windows, buttons, labels, text fields, dialogues, menus, and more.
Tkinter is:
-
Built-in (comes with Python)
-
Easy to use
-
Cross-platform
-
Used to create apps like
✔ Calculator
✔ Notepad
✔ Form applications
✔ Games
Creating Your First Tkinter Window
Example 1: Basic Window
import tkinter as tk
root = tk.Tk()
root.title("My First Tkinter App")
root.geometry("300x200")
root.mainloop()
Explanation
-
Tk()→ creates main window -
title()→ sets window title -
geometry()→ width x height -
mainloop()→ keeps the window open
Tkinter Widgets (Components)
Tkinter provides several built-in widgets. Below are the most commonly used ones with examples.
1. Label Widget
Displays text or images.
label = tk.Label(root, text="Hello Tkinter!", font=("Arial", 16))
label.pack()
2. Button Widget
Clickable button.
def click_me():
print("Button Clicked!")
button = tk.Button(root, text="Click Me", command=click_me)
button.pack()
3. Entry Widget
Used to take input from the user.
entry = tk.Entry(root, width=20)
entry.pack()
4. Text Widget
Multiline text editor.
text = tk.Text(root, height=5, width=30)
text.pack()
5. Checkbutton
check = tk.Checkbutton(root, text="Accept Terms")
check.pack()
6. Radio button
var = tk.StringVar()
tk.Radiobutton(root, text="Male", value="Male", variable=var).pack()
tk.Radiobutton(root, text="Female", value="Female", variable=var).pack()
7. Listbox
listbox = tk.Listbox(root)
listbox.insert(1, "Python")
listbox.insert(2, "Java")
listbox.pack()
Layout Managers in Tkinter
Tkinter provides 3 layout managers:
1. pack()
Arranges widgets vertically or horizontally.
2. grid()
Arranges widgets in rows and columns.
Example:
tk.Label(root, text="Name:").grid(row=0, column=0)
tk.Entry(root).grid(row=0, column=1)
3. place()
Place widgets at specific x and y coordinates.
Example:
button.place(x=100, y=50)
Common Inbuilt Tkinter Functions
| Function | Description |
|---|---|
title() |
Sets window title |
geometry() |
Sets window size |
resizable() |
Enables/disables resizing |
config() |
Changes widget properties |
destroy() |
Closes window |
pack() |
Layout manager |
grid() |
Grid layout |
place() |
Coordinate layout |
mainloop() |
Starts GUI loop |
Mini Project: Simple Tkinter Login Form
import tkinter as tk
root = tk.Tk()
root.title("Login Form")
root.geometry("300x200")
tk.Label(root, text="Username").pack()
username = tk.Entry(root)
username.pack()
tk.Label(root, text="Password").pack()
password = tk.Entry(root, show="*")
password.pack()
def login():
print("Username:", username.get())
print("Password:", password.get())
tk.Button(root, text="Login", command=login).pack()
root.mainloop()
Advantages of Tkinter
✔ Easy to learn
✔ Built into Python
✔ Cross-platform
✔ Large community support
✔ Fast prototyping
Applications Built Using Tkinter
-
Desktop calculators
-
Notepad/Text editor
-
Simple games
-
Registration forms
-
Attendance systems
-
Data visualization tools
Conclusion
Tkinter is one of the most powerful yet easiest libraries for creating GUI applications in Python. By understanding widgets, layout managers, and event handling, anyone can quickly build functional desktop apps.
Comments
Post a Comment