Tkinter’s Radiobutton
widget offers a user-friendly way to present a set of mutually exclusive options. This tutorial explores its functionalities, guiding you through creating, customizing, and responding to user selections.
Table of Contents:
1. Creating Radio Buttons
To create radio buttons, each needs to be linked to a shared variable (typically a StringVar
). This ensures only one button within the group can be selected at a time. Here’s how to create a basic set:
import tkinter as tk
root = tk.Tk()
root.title("Tkinter Radiobutton Example")
selected_option = tk.StringVar(value="Option A")
radio_button_a = tk.Radiobutton(root, text="Option A", variable=selected_option, value="Option A")
radio_button_a.pack(anchor=tk.W)
radio_button_b = tk.Radiobutton(root, text="Option B", variable=selected_option, value="Option B")
radio_button_b.pack(anchor=tk.W)
radio_button_c = tk.Radiobutton(root, text="Option C", variable=selected_option, value="Option C")
radio_button_c.pack(anchor=tk.W)
root.mainloop()
This code creates three radio buttons. The variable
argument links them, and the value
attribute assigns a value to each button’s selection.
2. Retrieving Selected Values
Accessing the selected value is straightforward. Simply use the get()
method on the shared variable:
import tkinter as tk
root = tk.Tk()
root.title("Get Radiobutton Value")
selected_option = tk.StringVar(value="Option A")
# ... (Radiobutton creation as above) ...
def get_selected():
print(f"Selected option: {selected_option.get()}")
button = tk.Button(root, text="Get Value", command=get_selected)
button.pack()
root.mainloop()
This example adds a button that prints the selected option when clicked. The get()
method retrieves the current value of the StringVar
.
3. Customizing Appearance
You can modify the radio button’s visual aspects. For instance, the indicatoron
option controls the indicator’s visibility, and selectcolor
changes the color when selected:
import tkinter as tk
root = tk.Tk()
root.title("Customize Radiobutton Appearance")
selected_option = tk.StringVar(value="Option A")
radio_button_a = tk.Radiobutton(root, text="Option A", variable=selected_option, value="Option A", indicatoron=False, selectcolor="lightgreen")
radio_button_a.pack(anchor=tk.W)
radio_button_b = tk.Radiobutton(root, text="Option B", variable=selected_option, value="Option B", selectcolor="lightblue")
radio_button_b.pack(anchor=tk.W)
root.mainloop()
This shows how to hide the indicator and change the selection color. Experiment with other styles to achieve your desired look.
4. Using Callback Functions
For a cleaner and more responsive approach, use the command
option to directly bind a function to each radio button:
import tkinter as tk
root = tk.Tk()
root.title("Radiobutton Callback")
def option_selected():
print(f"Selected option: {selected_option.get()}")
selected_option = tk.StringVar(value="Option A")
radio_button_a = tk.Radiobutton(root, text="Option A", variable=selected_option, value="Option A", command=option_selected)
radio_button_a.pack(anchor=tk.W)
radio_button_b = tk.Radiobutton(root, text="Option B", variable=selected_option, value="Option B", command=option_selected)
radio_button_b.pack(anchor=tk.W)
root.mainloop()
This method executes option_selected
whenever a button is selected, providing a more efficient and elegant solution for handling user interactions.