The Radiobutton widget in Tkinter allows you to create a set of mutually exclusive options. This means that users can select only one option at a time from a group of radiobuttons. Radiobuttons are often used in settings menus or questionnaires, where a single choice is required.
w = Radiobutton(parent, options)
• parent: The parent widget in which the radiobutton is placed (such as a window or frame).
• options: These define the appearance and behavior of the radiobutton, which are listed below.
| SN | Option | Description |
|---|---|---|
| 1 | activebackground | Background color of the radiobutton when it has focus. |
| 2 | activeforeground | Font color of the radiobutton text when it has focus. |
| 3 | anchor | Specifies the exact position of the text within the radiobutton if there's extra space. Default is CENTER. |
| 4 | bg | Background color of the radiobutton. |
| 5 | bitmap | Display a bitmap (graphical) image on the radiobutton instead of text. |
| 6 | borderwidth | Thickness of the radiobutton border. |
| 7 | command | Function to be called when the radiobutton’s state changes (when selected). |
| 8 | cursor | Type of mouse pointer to display when hovering over the radiobutton (e.g., arrow, dot). |
| 9 | font | Font style of the radiobutton text. |
| 10 | fg | Foreground (text) color of the radiobutton. |
| 11 | height | Vertical size of the radiobutton (in lines, not pixels). |
| 12 | highlightcolor | Color of the radiobutton when it has focus. |
| 13 | highlightbackground | Color of the radiobutton's highlight when not focused. |
| 14 | image | Image to display on the radiobutton instead of text. |
| 15 | justify | How to justify multi-line text. Options: CENTER, LEFT, RIGHT. |
| 16 | padx | Horizontal padding between the text/image and the radiobutton border. |
| 17 | pady | Vertical padding between the text/image and the radiobutton border. |
| 18 | relief | Border style of the radiobutton (e.g., FLAT, RAISED, SUNKEN). |
| 19 | selectcolor | Background color of the radiobutton when it is selected. |
| 20 | selectimage | Image to display when the radiobutton is selected. |
| 21 | state | State of the radiobutton (NORMAL or DISABLED). |
| 22 | text | Text to display next to the radiobutton. |
| 23 | textvariable | Variable to control the text dynamically. It is linked to a StringVar. |
| 24 | underline | Underlines the nth character of the text (default: -1, which means no underline). |
| 25 | value | The value assigned to the control variable when the radiobutton is selected. |
| 26 | variable | Control variable (IntVar or StringVar) shared among radiobuttons to keep track of the selected option. |
| 27 | width | Horizontal size of the radiobutton (in characters, not pixels). |
| 28 | wraplength | Wraps the text if it exceeds the specified number of characters per line. |
| SN | Method | Description |
|---|---|---|
| 1 | deselect() | Deselects the radiobutton. |
| 2 | flash() | Flashes the radiobutton between its active and normal colors a few times. |
| 3 | invoke() | Calls the associated command of the radiobutton (if any). |
| 4 | select() | Selects the radiobutton. |
from tkinter import *
# Create the main window
root = Tk()
root.title("Radiobutton Example")
# Control variable to store the selected option
selected_option = StringVar(value="Option 1")
# Function to display the selected option
def show_selection():
print(f"Selected: {selected_option.get()}")
# Create radiobuttons
radiobutton1 = Radiobutton(root, text="Option 1", variable=selected_option, value="Option 1", command=show_selection)
radiobutton1.pack(anchor=W)
radiobutton2 = Radiobutton(root, text="Option 2", variable=selected_option, value="Option 2", command=show_selection)
radiobutton2.pack(anchor=W)
radiobutton3 = Radiobutton(root, text="Option 3", variable=selected_option, value="Option 3", command=show_selection)
radiobutton3.pack(anchor=W)
# Run the Tkinter loop
root.mainloop()
• variable: Tracks the currently selected radiobutton (selected_option).
• value: The value of each radiobutton when selected.
• command: Function (show_selection) called when a radiobutton is clicked, printing the selected option.
This widget is useful for scenarios where users need to make a single choice from multiple options.