In this lesson we want to learn about Python PySide6 QRadioButton, Graphical User Interfaces (GUIs) are a great way to interact with our programs. They provide the user with a visual way of interacting with the program’s functions and data. PySide6 is a Python binding for the Qt toolkit that can be used to build GUI applications in Python. One of the widgets that PySide6 provide is the QRadioButton widget. In this lesson, we’ll explore what QRadioButtons are, how they work, and how you can use them to build nice GUIs.
What is a QRadioButton ?
A QRadioButton is a button that can be selected by the user to choose from a set of mutually exclusive options. Only one radio button in a set can be selected at any given time. Radio buttons are commonly used in GUIs to provide the user with a way to choose from a set of options.
How to Create QRadioButtons in PySide6
To create a QRadioButton in PySide6, you can use the QRadioButton
class. this i an example of how to create a radio button in python pyside6:
1 2 3 4 5 6 7 8 9 10 |
from PySide6.QtWidgets import QApplication, QRadioButton, QWidget app = QApplication([]) widget = QWidget() radio_button = QRadioButton("Option 1", widget) radio_button.move(50, 50) widget.show() app.exec() |
In this example, we creates a QRadioButton with the label “Option 1”. We set the parent of the radio button to be the widget
object, which is a QWidget that we’ll use to display the radio button. After that we use the move()
method to position the radio button at coordinates (50, 50) within the widget. Finally, we show the widget and start the event loop with app.exec()
.
Run the complete code this will be the result.

You can create multiple radio buttons and add them to the same parent widget to create a set of mutually exclusive options. Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
from PySide6.QtWidgets import QApplication, QRadioButton, QVBoxLayout, QWidget app = QApplication([]) widget = QWidget() layout = QVBoxLayout(widget) option1 = QRadioButton("Option 1", widget) option2 = QRadioButton("Option 2", widget) option3 = QRadioButton("Option 3", widget) layout.addWidget(option1) layout.addWidget(option2) layout.addWidget(option3) widget.show() app.exec() |
In this example we have created three radio buttons and add them to a vertical layout using QVBoxLayout
class. The addWidget()
method is used to add radio buttons to the layout in order that they should appear. By default, only one radio button in the set can be selected at any given time.
Run the code and this will be the result.

Retrieving the Selected QRadioButton
To retrieve the selected radio button from a set of radio buttons, you can use the isChecked()
method of the QRadioButton
class. Here’s an example:
1 2 3 4 5 6 |
if option1.isChecked(): print("Option 1 selected") elif option2.isChecked(): print("Option 2 selected") elif option3.isChecked(): print("Option 3 selected") |
In this example, we check each radio button in turn using the isChecked()
method. The isChecked()
method returns True
if the radio button is selected, and False
otherwise.
This is complete example on Python PySide6 QRadioButton
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
from PySide6.QtWidgets import QApplication, QRadioButton, QVBoxLayout, QWidget class RadioButtons(QWidget): def __init__(self): super().__init__() layout = QVBoxLayout() self.option1 = QRadioButton("Option 1") self.option2 = QRadioButton("Option 2") self.option3 = QRadioButton("Option 3") layout.addWidget(self.option1) layout.addWidget(self.option2) layout.addWidget(self.option3) self.setLayout(layout) self.option1.toggled.connect(self.selected_option) self.option2.toggled.connect(self.selected_option) self.option3.toggled.connect(self.selected_option) def selected_option(self): if self.option1.isChecked(): print("Option 1 selected") elif self.option2.isChecked(): print("Option 2 selected") elif self.option3.isChecked(): print("Option 3 selected") if __name__ == '__main__': app = QApplication([]) widget = RadioButtons() widget.show() app.exec() |
In this example, we define new RadioButtons
class that inherits from QWidget
. In the __init__
method, we create a vertical layout with three radio buttons with labels “Option 1”, “Option 2”, and “Option 3”. after that we have added the radio buttons to the layout and set the layout as the layout of the widget, or we can say the main window layout.
After that we connect the toggled
signal of each radio button to a single slot selected_option
using the connect()
method. The toggled
signal is emitted whenever checked state of the radio button changes. In the selected_option
method, we check which radio button is selected using the isChecked()
method of each radio button, and print a message indicating which option is selected.
at the end we creates an instance of the RadioButtons
class, show it, and start the event loop with app.exec()
. This results in a window containing a set of radio buttons that the user can interact with.
By using object-oriented programming, we can encapsulate the logic for creating and handling radio buttons in a single class, this makes our code more modular and easier to maintain.
Final Thoughts
QRadioButtons are a useful widget for building interactive GUIs in PySide6. They allow the user to choose from a set of mutually exclusive options. You can create a set of radio buttons by adding them to the same parent widget, and retrieve the selected radio button using the isChecked()
method. With these tools, you can create powerful and interactive GUIs in Python using PySide6.