PyQt5 Tutorials

PyQt5 Radio Buttons: A Comprehensive Guide

Spread the love

This tutorial provides a comprehensive guide to using PyQt5’s QRadioButton widget. We’ll cover the basics, explore the setChecked method, and delve into creating groups of radio buttons for mutually exclusive selection. Let’s get started!

Table of Contents:

  1. Basic QRadioButton Example
  2. The setChecked Method
  3. Creating Radio Button Groups
  4. Handling User Selection

1. Basic QRadioButton Example

The QRadioButton allows users to select one option from a set. Here’s a simple example:


import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QRadioButton, QVBoxLayout

app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("PyQt5 QRadioButton Example")

layout = QVBoxLayout()
label = QLabel("Select an option:")
layout.addWidget(label)

radio1 = QRadioButton("Option 1")
radio2 = QRadioButton("Option 2")
radio3 = QRadioButton("Option 3")

layout.addWidget(radio1)
layout.addWidget(radio2)
layout.addWidget(radio3)

window.setLayout(layout)
window.show()
sys.exit(app.exec_())

This creates three independent radio buttons. To check which is selected, use the isChecked() method.

2. The setChecked Method

Programmatically select a radio button using setChecked(True) or deselect with setChecked(False).


import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QRadioButton, QVBoxLayout, QPushButton

# ... (previous code) ...

button = QPushButton("Select Option 2")
button.clicked.connect(lambda: radio2.setChecked(True))

layout.addWidget(button)

# ... (rest of the code) ...

This adds a button that selects “Option 2” when clicked.

3. Creating Radio Button Groups

For mutually exclusive selection, use QButtonGroup. This ensures only one button in the group can be selected at a time.


import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QRadioButton, QVBoxLayout, QButtonGroup

app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("PyQt5 QRadioButton Group Example")

layout = QVBoxLayout()
label = QLabel("Select an option:")
layout.addWidget(label)

button_group = QButtonGroup()
radio1 = QRadioButton("Option 1")
radio2 = QRadioButton("Option 2")
radio3 = QRadioButton("Option 3")

button_group.addButton(radio1, 1)
button_group.addButton(radio2, 2)
button_group.addButton(radio3, 3)

layout.addWidget(radio1)
layout.addWidget(radio2)
layout.addWidget(radio3)

window.setLayout(layout)
window.show()
sys.exit(app.exec_())

4. Handling User Selection

Efficiently handle user selection using the buttonClicked signal:


button_group.buttonClicked.connect(lambda button: print(f"Selected option: {button_group.id(button)}"))

This prints the ID (assigned when adding to the group) of the selected button. Replace the print statement with your desired action.

This improved approach avoids manually checking each button’s state and provides a more robust and organized solution for managing radio button selections in your PyQt5 applications.

Leave a Reply

Your email address will not be published. Required fields are marked *