GUI Programming with Tkinter
Tkinter is Python's standard library for building graphical user interfaces (GUIs). It provides
tools to create windows, dialogs, buttons, menus, and other GUI elements.
1. Tkinter Introduction
What is Tkinter?
o A built-in Python library for GUI development.
o Based on the Tk GUI toolkit.
o Cross-platform: Works on Windows, macOS, and Linux.
Why use Tkinter?
o Easy to learn and use.
o No need to install; it comes pre-installed with Python.
o Provides a wide range of widgets like buttons, labels, textboxes, etc.
2. Tkinter and Python Programming
a) Basic Structure of a Tkinter Program
A simple Tkinter program involves:
1. Creating the main window.
2. Adding widgets to the window.
3. Running the application loop.
Example: Basic Tkinter Window
import tkinter as tk
# Create the main window
root = tk.Tk()
root.title("Hello Tkinter")
root.geometry("400x300") # Set window size
# Run the main event loop
root.mainloop()
3. Tk Widgets
Widgets are the building blocks of a GUI application in Tkinter. Below are some commonly
used widgets:
a) Label
Displays text or images.
label = tk.Label(root, text="Welcome to Tkinter!")
label.pack() # Add to window
b) Button
Used to trigger an action when clicked.
def on_click():
print("Button clicked!")
button = tk.Button(root, text="Click Me", command=on_click)
button.pack()
c) Entry
Single-line text input.
entry = tk.Entry(root)
entry.pack()
d) Text
Multi-line text input.
text = tk.Text(root, height=5, width=30)
text.pack()
e) Frame
A container widget to group other widgets.
frame = tk.Frame(root)
frame.pack()
f) Menu
Adds dropdown or popup menus.
menu = tk.Menu(root)
root.config(menu=menu)
menu.add_command(label="File")
menu.add_command(label="Edit")
4. Tkinter Examples
Example 1: A Basic Application
import tkinter as tk
# Create main window
root = tk.Tk()
root.title("Basic App")
root.geometry("300x200")
# Add a label
label = tk.Label(root, text="Hello, Tkinter!", font=("Arial", 16))
label.pack()
# Add a button
def greet():
label.config(text="Button Clicked!")
button = tk.Button(root, text="Click Me", command=greet)
button.pack()
# Start the application
root.mainloop()
Example 2: Login Form
import tkinter as tk
def login():
username = entry_user.get()
password = entry_pass.get()
print(f"Username: {username}, Password: {password}")
# Main window
root = tk.Tk()
root.title("Login Form")
root.geometry("300x150")
# Widgets
tk.Label(root, text="Username:").grid(row=0, column=0)
entry_user = tk.Entry(root)
entry_user.grid(row=0, column=1)
tk.Label(root, text="Password:").grid(row=1, column=0)
entry_pass = tk.Entry(root, show="*")
entry_pass.grid(row=1, column=1)
button = tk.Button(root, text="Login", command=login)
button.grid(row=2, columnspan=2)
# Run the app
root.mainloop()
Example 3: Calculator
import tkinter as tk
def calculate():
try:
result = eval(entry.get())
entry.delete(0, tk.END)
entry.insert(tk.END, str(result))
except:
entry.delete(0, tk.END)
entry.insert(tk.END, "Error")
# Main window
root = tk.Tk()
root.title("Calculator")
root.geometry("300x200")
# Input field
entry = tk.Entry(root, font=("Arial", 18))
entry.pack()
# Buttons
for char in "1234567890+-*/=":
btn = tk.Button(root, text=char, command=lambda c=char:
entry.insert(tk.END, c) if c != '=' else calculate())
btn.pack(side=tk.LEFT)
# Run app
root.mainloop()
5. Python Programming with an IDE
An IDE (Integrated Development Environment) simplifies Python programming by
providing:
Code editing: Syntax highlighting, auto-completion.
Debugging: Helps identify and fix errors.
Testing: Run and test code easily.
Popular Python IDEs:
1. PyCharm:
o Advanced features for professional developers.
o Built-in support for Tkinter.
o Free Community Edition available.
2. Visual Studio Code:
o Lightweight and extensible.
o Requires Python extension for advanced features.
3. IDLE:
o Comes pre-installed with Python.
o Basic and suitable for beginners.
4. Jupyter Notebook:
o Ideal for data visualization and interactive coding.
Steps to Use IDE for Tkinter:
1. Install the IDE:
o Download and install your preferred IDE (e.g., PyCharm or VS Code).
2. Create a New Python File:
o Use the IDE's interface to create and save a .py file.
3. Write Tkinter Code:
o Use the IDE's editor to write the Tkinter program.
4. Run the Application:
o Use the Run or Play button to execute the script.
5. Debugging:
o Use breakpoints and the debugging tools to test and fix errors.