9.2 Tkinter module
The Python Tkinter module is a standard library used for creating graphical user interfaces (GUIs) in Python applications. It provides a range of tools (widgets) such as buttons, labels, text boxes, and more, which can be used to design and build interactive windows and forms. Tkinter allows developers to add visual elements and respond to user events like clicks and key presses, making it easier to create user-friendly applications.
9.2a Getting started installing Tkinter
If you are using a standard Python installation, Tkinter is usually included. To verify if Tkinter is installed, you can try importing it in Python. For this topic make sure to use Thonny IDE since the Tkinter module is not supported by trinket.io.
9.2b Basic Tkinter application
Let’s create a basic Tkinter window with this step-by-step guide.
-
Import Tkinter module - Import the Tkinter module (note that in Python 3.x, it’s tkinter with a lowercase 't').
-
Create the main window - Create the main application window.
-
Add widgets to the window - Widgets are the elements that make up the GUI (buttons, labels, text boxes, etc.).
-
Start the main loop - The main loop waits for events (such as button clicks) and updates the GUI accordingly.
Example 9.2.1 - Here’s a simple example with Tkinter module
import tkinter as tk
# Create the main window
root = tk.Tk()
# Set the window title
root.title("My Tkinter App")
# Set the window size
root.geometry("300x200")
# Add a label widget
label = tk.Label(root, text="Hello, Tkinter!")
label.pack(pady=20) # pady adds vertical padding
# Add a button widget
button = tk.Button(root, text="Click Me", command=lambda: print("Button clicked!"))
button.pack(pady=20)
# Start the main loop
root.mainloop()
9.2c Adding more widgets
Tkinter provides a variety of widgets. Here are a few common ones:
- Labels - Display text or images.
- Buttons - Perform an action when clicked.
- Entries - Single-line text boxes.
- Text - Multi-line text boxes.
- Checkbuttons - Checkbox widgets.
- Radiobuttons - Radio button widgets.
- Listboxes - List of selectable items.
Example 9.2.2 - with multiple widgets
import tkinter as tk
def on_button_click():
label.config(text="Button Clicked!")
root = tk.Tk()
root.title("Widget Example")
root.geometry("400x300")
label = tk.Label(root, text="Welcome to Tkinter!")
label.pack(pady=10)
entry = tk.Entry(root)
entry.pack(pady=10)
button = tk.Button(root, text="Click Me", command=on_button_click)
button.pack(pady=10)
checkbutton = tk.Checkbutton(root, text="Check me")
checkbutton.pack(pady=10)
radio_value = tk.IntVar()
radiobutton1 = tk.Radiobutton(root, text="Option 1", variable=radio_value, value=1)
radiobutton2 = tk.Radiobutton(root, text="Option 2", variable=radio_value, value=2)
radiobutton1.pack(pady=5)
radiobutton2.pack(pady=5)
listbox = tk.Listbox(root)
listbox.insert(1, "Item 1")
listbox.insert(2, "Item 2")
listbox.insert(3, "Item 3")
listbox.pack(pady=10)
root.mainloop()
9.2d Layout Management
Tkinter provides three layout managers:
- pack() - Packs widgets in order.
- grid() - Places widgets in a grid (rows and columns).
- place() - Places widgets at an absolute position.
Example 9.2.3 - Using grid()
import tkinter as tk
root = tk.Tk()
root.title("Grid Layout Example")
root.geometry("300x200")
tk.Label(root, text="Name:").grid(row=0, column=0, padx=10, pady=10)
tk.Entry(root).grid(row=0, column=1, padx=10, pady=10)
tk.Label(root, text="Email:").grid(row=1, column=0, padx=10, pady=10)
tk.Entry(root).grid(row=1, column=1, padx=10, pady=10)
tk.Button(root, text="Submit").grid(row=2, column=0, columnspan=2, pady=10)
root.mainloop()
9.2e Event Handling
Events are actions performed by the user, such as mouse clicks or key presses. You can bind events to widgets to make your GUI interactive.
Example 9.2.4 - Event binding
import tkinter as tk
def on_key_press(event):
label.config(text=f"Key pressed: {event.char}")
root = tk.Tk()
root.title("Event Binding Example")
root.geometry("300x200")
label = tk.Label(root, text="Press any key")
label.pack(pady=20)
root.bind("<KeyPress>", on_key_press)
root.mainloop()
Note
This should give you a good starting point to explore the capabilities of Tkinter. You can refer to the Tkinter documentation for more detailed information on widgets, layouts, and event handling.