QRadioButton Widget in PyQt6

Radio buttons are common widgets that we use in our UI applications which allow users to select an option from a group.

There are pretty easy and simple methods to create Radio buttons in PyQt6.

In this article, I will show you a couple of methods to create Radio buttons in PyQt6 and also about event handling.

QRadioButton Widget in PyQt6

QRadioButton widgets are UI elements that allow users to select exactly one option from a set of choices. They appear as small circles that become filled when selected.

Radio buttons are ideal when:

  • Users must choose exactly one option from a small set of mutually exclusive choices
  • The options are simple enough to display all at once
  • Visual clarity about the currently selected option is important

Read How to Handle Button Click Events Using Signals and Slots in PyQt6?

Create QRadioButton Widget in PyQt6

Let me explain to you the important methods for creating the QRadioButton widget in PyQt6.

Method 1: Direct Creation (Basic Method)

The most simple way of creating Radio buttons is simply to create a widget object directly using its class constructor and then add it to your layout.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QRadioButton

class RadioButtonExample(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        # Create layout
        layout = QVBoxLayout()

        # Create a single radio button
        radio = QRadioButton("Option 1")
        layout.addWidget(radio)

        # Set the layout to the main window
        self.setLayout(layout)

        # Set window title and size
        self.setWindowTitle("QRadioButton Example")
        self.setGeometry(100, 100, 300, 100)
        self.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = RadioButtonExample()
    sys.exit(app.exec())

I executed the above example code and added the screenshot below.

Create QRadioButton Widget in PyQt6

We directly create a QRadioButton widget with the text “Option 1” and then add it to a layout.

Check out How to Arrange Widgets Using QGridLayout in PyQt6?

Method 2: Grouped Radio Buttons using QButtonGroup

QButtonGroup is used to group radio buttons together so that only one button in the group can be checked at a time.

import sys
from PyQt6.QtWidgets import (
    QApplication, QWidget, QVBoxLayout, QRadioButton, QButtonGroup
)

class SimpleRadioButtons(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        layout = QVBoxLayout()

        # Create radio buttons
        radio1 = QRadioButton("Male")
        radio2 = QRadioButton("Female")

        # Add to layout
        layout.addWidget(radio1)
        layout.addWidget(radio2)

        # Group the radio buttons
        group = QButtonGroup(self)
        group.addButton(radio1)
        group.addButton(radio2)

        self.setLayout(layout)
        self.setWindowTitle("Simple Radio Buttons")
        self.setGeometry(100, 100, 300, 100)
        self.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = SimpleRadioButtons()
    sys.exit(app.exec())

I executed the above example code and added the screenshot below.

QRadioButton Widget in PyQt6

Displays two radio buttons (“Male” and “Female”) in a vertical layout. Uses QButtonGroup to ensure only one can be selected at a time.

Read How to Use QVBoxLayout in PyQt6 for Vertical Layouts?

Method 3: Radio Buttons inside Layouts or GroupBoxes

This method uses a QGroupBox to visually group radio buttons together with a titled border. The radio buttons are organized within a layout inside the group box.

import sys
from PyQt6.QtWidgets import (
    QApplication, QWidget, QVBoxLayout, QRadioButton, QGroupBox
)

class GenderSelectionApp(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        # Create a group box
        group_box = QGroupBox("Select Gender")

        # Create a vertical layout and add radio buttons
        vbox = QVBoxLayout()
        vbox.addWidget(QRadioButton("Male"))
        vbox.addWidget(QRadioButton("Female"))

        # Set the layout to the group box
        group_box.setLayout(vbox)

        # Main layout of the window
        main_layout = QVBoxLayout()
        main_layout.addWidget(group_box)

        self.setLayout(main_layout)
        self.setWindowTitle("Gender Selection")
        self.setGeometry(100, 100, 300, 150)
        self.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = GenderSelectionApp()
    sys.exit(app.exec())

I executed the above example code and added the screenshot below.

How to Create QRadioButton Widget in PyQt6

This approach provides a visual boundary around related radio buttons with a descriptive title, making the UI more organized and user-friendly.

Check out How to Use QHBoxLayout in PyQt6 for Horizontal Layouts?

Method 4: Dynamically Creating Radio Buttons from a List

This method creates radio buttons programmatically from a list of options. It’s useful when you have a variable number of choices or when the options are determined at runtime.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QRadioButton

class RadioButtonDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QRadioButton with Loop")
        self.setGeometry(100, 100, 300, 150)
        self.init_ui()

    def init_ui(self):
        layout = QVBoxLayout()

        options = ["Option A", "Option B", "Option C"]
        for option in options:
            rb = QRadioButton(option)
            layout.addWidget(rb)

        self.setLayout(layout)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = RadioButtonDemo()
    window.show()
    sys.exit(app.exec())

This example creates a window with three radio buttons: Option A, Option B, and Option C.

Read How to Create QLineEdit Widget in PyQt6?

Check Which Radio Button is Selected in PyQt6

Detecting which radio button is selected is essential for responding to user choices in your application. Now I will explain how to check which radio button is selected.

from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QRadioButton, QButtonGroup, QPushButton, QMessageBox

class RadioButtonDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        layout = QVBoxLayout()

        # Create radio buttons
        self.radio1 = QRadioButton("Python")
        self.radio2 = QRadioButton("JavaScript")
        self.radio3 = QRadioButton("Java")

        # Group the buttons
        self.group = QButtonGroup()
        self.group.addButton(self.radio1)
        self.group.addButton(self.radio2)
        self.group.addButton(self.radio3)

        # Add to layout
        layout.addWidget(self.radio1)
        layout.addWidget(self.radio2)
        layout.addWidget(self.radio3)

        # Add a submit button
        submit_btn = QPushButton("Submit")
        submit_btn.clicked.connect(self.check_selection)
        layout.addWidget(submit_btn)

        self.setLayout(layout)
        self.setWindowTitle("Check Selected Radio Button")

    def check_selection(self):
        for button in self.group.buttons():
            if button.isChecked():
                QMessageBox.information(self, "Selection", f"You selected: {button.text()}")
                break

app = QApplication([])
window = RadioButtonDemo()
window.show()
app.exec()

I executed the above example code and added the screenshot below.

PyQt6 QRadioButton Widget

This example efficiently shows how to check which radio button is selected using a loop through the QButtonGroup. It’s ideal for handling user input in forms, quizzes, or settings

Use Case: Create a Survey Form with Multiple Questions

Let me explain to you a real-world use case which is a radio button in survey forms, where users need to select one option from multiple choices for each question.

from PyQt6.QtWidgets import (
    QApplication, QWidget, QVBoxLayout, QLabel, QRadioButton,
    QButtonGroup, QPushButton, QMessageBox, QGroupBox
)

class SurveyForm(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Customer Feedback Survey")
        self.setGeometry(100, 100, 400, 300)
        self.init_ui()

    def init_ui(self):
        main_layout = QVBoxLayout()

        # Question 1
        q1_box = QGroupBox("1. How satisfied are you with our service?")
        q1_layout = QVBoxLayout()
        self.q1_group = QButtonGroup()

        for i, option in enumerate(["Very Satisfied", "Satisfied", "Neutral", "Unsatisfied"]):
            rb = QRadioButton(option)
            self.q1_group.addButton(rb, i)
            q1_layout.addWidget(rb)
        q1_box.setLayout(q1_layout)

        # Question 2
        q2_box = QGroupBox("2. Would you recommend us to a friend?")
        q2_layout = QVBoxLayout()
        self.q2_group = QButtonGroup()

        for i, option in enumerate(["Yes", "Maybe", "No"]):
            rb = QRadioButton(option)
            self.q2_group.addButton(rb, i)
            q2_layout.addWidget(rb)
        q2_box.setLayout(q2_layout)

        # Submit Button
        submit_btn = QPushButton("Submit")
        submit_btn.clicked.connect(self.show_results)

        # Add everything to the main layout
        main_layout.addWidget(q1_box)
        main_layout.addWidget(q2_box)
        main_layout.addWidget(submit_btn)

        self.setLayout(main_layout)

    def show_results(self):
        q1_selected = self.q1_group.checkedButton()
        q2_selected = self.q2_group.checkedButton()

        if q1_selected and q2_selected:
            QMessageBox.information(
                self,
                "Survey Result",
                f"Q1: {q1_selected.text()}\nQ2: {q2_selected.text()}"
            )
        else:
            QMessageBox.warning(
                self,
                "Incomplete",
                "Please answer all questions before submitting."
            )

app = QApplication([])
window = SurveyForm()
window.show()
app.exec()

This shows actual survey forms used in customer feedback, HR surveys, or event registrations, making it a highly practical example.

Check out How to Create QPushButton Widget in PyQt6?

In this tutorial, I explained QRadioButton widget in PyQt6. I discussed the easy direct method, grouped radio buttons, radio button inside layouts, and dynamic creation of radio buttons from a list. I also covered how to check which radio button is selected and a real-world use case.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.