
- PyQt Tutorial
- PyQt - Home
- PyQt - Introduction
- PyQt - Environment
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Meta Objects
- PyQt Signals & Slots
- PyQt - Signals and Slots
- PyQt - Support and Signals
- PyQt - Unbound and Bound Signals
- PyQt - New Signals with PyQtSignal
- PyQt - Connecting, Disconnecting, & Emitting Signals
- PyQt - Slot decorator
- PyQt - Slot Connection
- PyQt Layouts
- PyQt - Layout Management
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt Basic Widgets
- PyQt - Basic Widgets
- PyQt - Qlabel Widget
- PyQt - QLineEdit Widget
- PyQt - QPushButton Widget
- PyQt - QRadioButton Widget
- PyQt - QCheckBox Widget
- PyQt - QComboBox Widget
- PyQt - QSpinBox Widget
- PyQt - QMessageBox
- PyQt - QDialogButtonBox Widget
- PyQt - QFontComboBox Widget
- PyQt - QDoubleSpinBox Widget
- PyQt - QToolBox Widget
- PyQt - QMenuBar, QMenu & Qaction Widgets
- PyQt - QToolTip
- PyQt - QInputDialog Widget
- PyQt - QFontDialog Widget
- PyQt - QDialog Widget
- PyQt - QFileDialog Widget
- PyQt - QTab Widget
- PyQt - QSplitter Widget
- PyQt - QDock Widget
- PyQt - QStatusBar Widget
- PyQt - QTabBar
- PyQt - QList Widget
- PyQt - QScrollBar Widget
- PyQt - QProgressBar
- PyQt - QCalendar Widget
- PyQt - QMessageBox Widget
- PyQt - QPlainTextEdit
- PyQt - QDateEdit
- PyQt - QDateTimeEdit
- PyQt - QTimeEdit
- PyQt - QTextEdit
- PyQt - QTextBrowser
- PyQt - QScrollArea
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QDialog Class
- PyQt Views
- PyQt - QColumnView
- PyQt - QTableView
- PyQt Drawing API
- PyQt - Drawing API
- PyQt - Drawing a Line
- PyQt - Drawing a Rectangle
- PyQt - Drawing a Triangle
- PyQt - Drawing a Circle
- PyQt - Drawing a Ellipse
- PyQt - Drawing a Polygon
- PyQt - Geometric Transformation
- PyQt - Drawing Effect
- PyQt Groups
- PyQt - QButtonGroup
- PyQt - QGroupBox
- PyQt Effects
- PyQt - Effects
- PyQt - Opacity Effect
- PyQt - QGraphicsBlur Effect
- PyQt - QGraphicsColorize Effect
- PyQt - QGraphicsDropShadow Effect
- PyQt Events
- PyQt - Event Handling
- PyQt - Drag & Drop Events
- PyQt - File Open Event
- PyQt - Action Event
- PyQt - Hide Event
- PyQt - Resize Event
- PyQt Database
- PyQt - Database Handling
- PyQt Essentials
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
PyQt - QButtonGroup
A group is a collection of similar objects. This grouping is established through classes like QButtonGroup or by arranging them using Layout Management. The UI component is more user-friendly when the user uses this class to build the application. Using QButtonGroup ensures that buttons behave together because it deals with the set of related options. For example, in a radio button group, only one option can be selected.
The following key purposes of QButtonGroup −
- It simplifies event handling.
- It makes an exclusive choice when used to select any single option.
- It helps the user to understand the relationship between different elements and makes the application more interactive.
- The arrangement and positioning of widgets can be controlled through layout management.
Components of QButtonGroup
Building the radio button using QButtonGroup involves various components −
QRadioButton − This is the actual button that builds the checkboxes to be checked or unchecked.
Note that, the function QRadioButton can be called through addWidget() in the layout section.
QRadioButton("option_name")
QButtonGroup − The class itself performs a built-in method i.e. used to build the radio button together. It ensures that only one radio button within the group can be checked at any given time.
QButtonGroup
Layout − The layout is the optional measure to use classes like QVBoxLayout and QHBoxLayout that arrange and organize the radio buttons in a specific order. It is useful when we have multiple radio buttons.
QVBoxLayout()
Orientation of Radio Button
- QHBoxLayout − This class is used to contruct horizontal layout for horizontal arrangement.
- QVBoxLayout − This class is used to contruct vertical layout for vertical arrangement.

Radio Button using QVBoxLayout
We can use PyQt library to create a simple GUI with radio buttons. The class QVBoxLayout creates a group of radio buttons for programming languages (C, C++, Java, Perl, Python) and when a radio button is toggled, the on_button_toggled method prints the selected language.
Example
Following example illustrate the code snippet of radio button using QVBoxLayout.
import sys from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QRadioButton, QButtonGroup class MyWidget(QWidget): def __init__(self): super().__init__() layout = QVBoxLayout(self) button_group = QButtonGroup(self) # If the user wants to perform an action on the options button_group.buttonToggled.connect(self.on_button_toggled) for text in ['C', 'C++', 'Java', 'Perl', 'Python']: radio_button = QRadioButton(text) layout.addWidget(radio_button) button_group.addButton(radio_button) def on_button_toggled(self, button, checked): if checked: print(f'Button "{button.text()}" is checked.') if __name__ == '__main__': app = QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec())
Output
The above code produces the following outcome −

Radio Button using QHBoxLayout
In PyQt, we create a window with choices (Linux, Windows, Mac OS) using radio buttons. It prints the chosen option when a button is clicked and displays the selected option when another button is pressed.
Example
Following example illustrates the code for users to select the option in the PyQt Window(horizontal orientation).
import sys from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QRadioButton, QButtonGroup, QPushButton class ButtonGroupExample(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): # Create a horizontal layout hbox = QHBoxLayout() # Create radio buttons radio_button1 = QRadioButton('Linux') radio_button2 = QRadioButton('Window') radio_button3 = QRadioButton('Mac Os') # Create a button group and add radio buttons to it button_group = QButtonGroup() button_group.addButton(radio_button1) button_group.addButton(radio_button2) button_group.addButton(radio_button3) # Set the default checked button radio_button1.setChecked(True) # Connect a slot to handle button clicks button_group.buttonClicked.connect(self.on_button_clicked) # Add radio buttons to the layout hbox.addWidget(radio_button1) hbox.addWidget(radio_button2) hbox.addWidget(radio_button3) # Add a button to show the selected option show_selected_button = QPushButton('Operating System') show_selected_button.clicked.connect(self.show_selected_option) hbox.addWidget(show_selected_button) # Set the layout for the main window self.setLayout(hbox) # Set window properties self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Horizontal Radio Button') def on_button_clicked(self, button): print(f'Button clicked: {button.text()}') def show_selected_option(self): selected_button = self.sender().group().checkedButton() if selected_button: print(f'Selected option: {selected_button.text()}') else: print('No option selected') if __name__ == '__main__': app = QApplication(sys.argv) window = ButtonGroupExample() window.show() sys.exit(app.exec())
Output
The above code produces the following Output −
