Featured Article

Python Radiobutton Example: Step-by-Step Guide

Nicole Jul 13, 2026

Python, with its ease of use and extensive libraries, is a popular choice for various applications, including Graphical User Interface (GUI) development. One common GUI element is the radio button, used to select one option from a group. Here, we'll provide an example of how to implement radio buttons in Python using the tkinter library.

Understanding FastAPI,Celery and RabbitMQ in simple terms
Understanding FastAPI,Celery and RabbitMQ in simple terms

Tkinter is Python's standard GUI toolkit, and it provides a simple and efficient way to create radio buttons in Python. Before starting, make sure you have it installed. It comes pre-installed with most Python distributions, but if not, you can install it via pip: `pip install tk`.

Python GUI examples (Tkinter Tutorial)
Python GUI examples (Tkinter Tutorial)

Creating Radio Buttons in Python

To create radio buttons in Python, you'll first need to import the tkinter library and create a window. Then, you can use the `ttk.Radiobutton` widget to create your radio buttons. Here's a simple example:

Python audio to text converter! ⭐️
Python audio to text converter! ⭐️

Python Radio Button Example

```python import tkinter as tk from tkinter import ttk root = tk.Tk() # Create a StringVar to store the selected option var = tk.StringVar() # Create Radio Buttons ttk.Radiobutton(root, text="Option 1", variable=var, value="1").pack(pady=5) ttk.Radiobutton(root, text="Option 2", variable=var, value="2").pack(pady=5) ttk.Radiobutton(root, text="Option 3", variable=var, value="3").pack(pady=5) # Function to handle the selection def selection(event): print("Selected option: ", var.get()) var.trace('w', selection) root.mainloop() ```

In this example, we create three radio buttons with the text "Option 1", "Option 2", and "Option 3". We store the selected option in a `StringVar` called "var". When a button is selected, the value is printed to the console.

W3Schools.com
W3Schools.com

Understanding the `StringVar` and `value` Attribute

`StringVar` is used to store string data in tkinter. It provides a way to know when the value has been changed, making it suitable for use with radio buttons. The `value` attribute of the radio button widget is used to set the button's unique state. When a button is selected, its `value` is set as the `StringVar` object.

Similarly, you can use `IntVar` or `DoubleVar` for integer or float values, respectively. However, for our purposes, `StringVar` is usually sufficient.

Python Ternary Operator with Best Example Programs
Python Ternary Operator with Best Example Programs

Handling Radio Button Selections

In the example, a trace is added to the `StringVar` object so that the `selection` function is called whenever the value changes. This function prints the selected option to the console. You can replace the `selection` function with your custom code to handle the selected options as needed.

Here's a slightly more complex example that uses radio buttons to choose a color. The selected color is applied to a label in real-time:

Managing Radio buttons in Tkinter GUI by setting and reading selected values with options to disable
Managing Radio buttons in Tkinter GUI by setting and reading selected values with options to disable

```python import tkinter as tk from tkinter import ttk root = tk.Tk() root.geometry("200x50") # Create a StringVar to store the selected color color_var = tk.StringVar() # Function to change the label's color def change_color(event): if color_var.get() == "Red": label.config fg="red" elif color_var.get() == "Green": label.config fg="green" elif color_var.get() == "Blue": label.config fg="blue" # Create Label label = ttk.Label(root, text="Select a color") label.pack(pady=10) # Create Radio Buttons ttk.Radiobutton(root, text="Red", variable=color_var, value="Red").pack(side=tk.LEFT, padx=5) ttk.Radiobutton(root, text="Green", variable=color_var, value="Green").pack(side=tk.LEFT, padx=5) ttk.Radiobutton(root, text="Blue", variable=color_var, value="Blue").pack(side=tk.LEFT, padx=5) # Add trace color_var.trace('w', change_color) root.mainloop() ```

In this example, we create three radio buttons for selecting colors. The `change_color` function updates the color of the label based on the selected radio button. The `trace` method is used to call this function whenever the `StringVar` object's value changes.

Python's tkinter library provides a simple and powerful way to create radio buttons and handle their selections. Whether you're creating a simple GUI application or a more complex system, these examples demonstrate how to effectively use radio buttons in Python.

an image of a computer screen with the text library emot clock
an image of a computer screen with the text library emot clock
🌍✨ PYTHON FOR WORKING WITH APIs: INTEGRATE WITH THE WEB ✨🌍
🌍✨ PYTHON FOR WORKING WITH APIs: INTEGRATE WITH THE WEB ✨🌍
Draw a Heart in Python Using Turtle Graphics ❤️
Draw a Heart in Python Using Turtle Graphics ❤️
Patient Information System in Python with Source Code and Database
Patient Information System in Python with Source Code and Database
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
9 Python Projects for Beginners (Build Your Portfolio Fast)
9 Python Projects for Beginners (Build Your Portfolio Fast)
Python PyQt Calculator GUI Program - EasyCodeBook.com
Python PyQt Calculator GUI Program - EasyCodeBook.com
a screenshot of a button with the text'click me'and an image of a black background
a screenshot of a button with the text'click me'and an image of a black background
Complete Python Built-in Functions with Examples
Complete Python Built-in Functions with Examples

Now that you understand the basics, explore other widgets and features of tkinter to create more dynamic and interactive GUIs. Happy coding!