PyQt - QRadioButton Widget



A QRadioButton class object presents a selectable button with a text label. It is commonly used when the user need to choose one option from a list of options. This widget is represented by a small circular button that can be toggled on (checked) or off (unchecked). This class is derived from QAbstractButton class.

pyqt qradiobutton inheritance

QRadioButton Auto-Exclusiove Behaviour

Radio buttons are autoexclusive by default. Hence, only one of the radio buttons in the parent window can be selected at a time. If one is selected, previously selected button is automatically deselected. Radio buttons can also be put in a QGroupBox or QButtonGroup to create more than one selectable fields on the parent window.

Signaling

Whenever a QRadioButton is toggled, it emits the toggled() signal. This signal can be connected to trigger actions in response to changes in the button's state. We can also use the isChecked() method to determine if a particular button is currently selected.

Text and Icons

Similar to other button-like widgets such as QPushButton, QRadioButton can display both text and optional icons. The text content can be set either during initialization or dynamically using the setText() method. Shortcut keys can also be specified within the text for accessibility purposes.

Methods of QRadioButton Class

The following listed methods of QRadioButton class are most commonly used.

Sr.No. Methods & Description
1

setChecked()

Changes the state of radio button

2

setText()

Sets the label associated with the button

3

text()

Retrieves the caption of button

4

isChecked()

Checks if the button is selected

Default signal associated with QRadioButton object is toggled(), although other signals inherited from QAbstractButton class can also be implemented.

Example

Here two mutually exclusive radio buttons are constructed on a top level window.

Default state of b1 is set to checked by the statement −

Self.b1.setChecked(True)

The toggled() signal of both the buttons is connected to btnstate() function. Use of lambda allows the source of signal to be passed to the function as an argument.

self.b1.toggled.connect(lambda:self.btnstate(self.b1))
self.b2.toggled.connect(lambda:self.btnstate(self.b2))

The btnstate() function checks state of button emitting toggled() signal.

import sys
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.QtWidgets import QWidget,QApplication, QRadioButton,QHBoxLayout

class Radiodemo(QWidget):

   def __init__(self, parent = None):
      super(Radiodemo, self).__init__(parent)
		
      layout = QHBoxLayout()
      self.b1 = QRadioButton("Button1")
      self.b1.setChecked(True)
      self.b1.toggled.connect(lambda:self.btnstate(self.b1))
      layout.addWidget(self.b1)
		
      self.b2 = QRadioButton("Button2")
      self.b2.toggled.connect(lambda:self.btnstate(self.b2))

      layout.addWidget(self.b2)
      self.setLayout(layout)
      self.setWindowTitle("RadioButton demo")
		
   def btnstate(self,b):
	
      if b.text() == "Button1":
         if b.isChecked() == True:
            print (b.text()+" is selected")
         else:
            print (b.text()+" is deselected")
				
      if b.text() == "Button2":
         if b.isChecked() == True:
            print (b.text()+" is selected")
         else:
            print (b.text()+" is deselected")
				
def main():

   app = QApplication(sys.argv)
   ex = Radiodemo()
   ex.show()
   sys.exit(app.exec())
	
if __name__ == '__main__':
   main()

Output

The above code produces the following output −

qradiobuttonX widget output
Button1 is deselected
Button2 is selected
Button2 is deselected
Button1 is selected

Example: Grouping Radio button

In the below example, we will create a group radio button using QButtonGroup to enforce exclusive selection within each group.

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

class RadioButtonsGroupExample(QWidget):
   def __init__(self):
      super().__init__()
      self.initUI()

   def initUI(self):
      layout = QVBoxLayout()
      self.group1 = QButtonGroup()
      self.group2 = QButtonGroup()

      self.radio_button1 = QRadioButton("Option 1")
      self.radio_button2 = QRadioButton("Option 2")
      self.radio_button3 = QRadioButton("Option 3")
      self.radio_button4 = QRadioButton("Option 4")

      layout.addWidget(self.radio_button1)
      layout.addWidget(self.radio_button2)
      layout.addWidget(self.radio_button3)
      layout.addWidget(self.radio_button4)

      self.group1.addButton(self.radio_button1)
      self.group1.addButton(self.radio_button2)

      self.group2.addButton(self.radio_button3)
      self.group2.addButton(self.radio_button4)

      self.setLayout(layout)

if __name__ == '__main__':
   app = QApplication(sys.argv)
   ex = RadioButtonsGroupExample()
   ex.show()
   sys.exit(app.exec())

Output

The above code produces the following output −

pyqt qradiobutton grouping example
Advertisements