🐍 Python UI Programming Series — Part 7: Advanced Widgets in Tkinter – Combobox, Checkbutton, Radiobutton, and More
📘 Overview
By now, you've built a multi-page, database-connected Tkinter app with validation and styling. Next, let’s explore advanced widgets that let users interact with your app in more dynamic ways.
In this part, you’ll learn how to use:
-
Combobox
(dropdown) -
Checkbutton
(toggle options) -
Radiobutton
(single choice selection) -
Spinbox
(numeric selection) -
Listbox
(multiple options)
🎛️ Why Use Advanced Widgets?
Advanced widgets:
-
Enhance interactivity and user experience
-
Make your UI feel more like modern apps
-
Reduce typing errors by using selectable input
✅ 1. Combobox – Dropdown Menu
from tkinter import ttk
root = tk.Tk()
root.title("Combobox Example")
ttk.Label(root, text="Choose a language:").pack(pady=5)
combo = ttk.Combobox(root, values=["Python", "JavaScript", "C++", "Java"])
combo.pack(pady=5)
combo.current(0) # Set default value
def show_choice():
messagebox.showinfo("Selected", f"You chose {combo.get()}")
ttk.Button(root, text="Submit", command=show_choice).pack(pady=10)
root.mainloop()
✅ 2. Checkbutton – Toggle Options
var_python = tk.IntVar()
check = tk.Checkbutton(root, text="I know Python", variable=var_python)
check.pack(pady=5)
def check_status():
status = "Yes" if var_python.get() else "No"
messagebox.showinfo("Checked?", f"Knows Python? {status}")
tk.Button(root, text="Check", command=check_status).pack(pady=5)
🧠 Use IntVar()
or BooleanVar()
to track check state.
✅ 3. Radiobutton – Single Selection
language = tk.StringVar(value="Python")
tk.Label(root, text="Favorite Language:").pack()
tk.Radiobutton(root, text="Python", variable=language, value="Python").pack()
tk.Radiobutton(root, text="Java", variable=language, value="Java").pack()
tk.Radiobutton(root, text="C++", variable=language, value="C++").pack()
def show_language():
messagebox.showinfo("Language", f"You selected {language.get()}")
tk.Button(root, text="Submit", command=show_language).pack(pady=10)
✅ 4. Spinbox – Numeric Selection
spin = tk.Spinbox(root, from_=1, to=10)
spin.pack(pady=5)
def show_number():
messagebox.showinfo("Selected", f"Number: {spin.get()}")
tk.Button(root, text="Get Number", command=show_number).pack()
🧮 Useful for quantities, age, or any numeric input.
✅ 5. Listbox – Multi-Select Option
listbox = tk.Listbox(root, selectmode="multiple")
langs = ["Python", "Java", "C++", "Go", "Rust"]
for lang in langs:
listbox.insert(tk.END, lang)
listbox.pack(pady=5)
def show_selected():
items = [listbox.get(i) for i in listbox.curselection()]
messagebox.showinfo("Selection", f"You selected: {', '.join(items)}")
tk.Button(root, text="Show Selected", command=show_selected).pack(pady=5)
🧠 Tip: Use tk.Variable()
Types for Dynamic Updates
-
IntVar()
— for integers (e.g., Checkbutton) -
StringVar()
— for text (e.g., Entry, Radiobutton) -
DoubleVar()
— for floats -
BooleanVar()
— for true/false values
These make it easy to link widget values to your app logic.
💡 Recap
In this part, you’ve learned:
-
How to use dropdowns, toggles, and selection inputs
-
How to track widget states using
tk.Variable()
-
When and why to use advanced widgets for better UX
🚀 Coming Up in Part 8:
"Packaging Your Tkinter App – Turn Your Python GUI into an Executable (.exe)"
-
Create standalone desktop apps using
pyinstaller
-
Distribute your app without requiring Python installation
No comments:
Post a Comment