PyQt QRadioButton
The QRadioButton widget is used to create buttons that the user can select. This button also includes one text label, just like a check box. (See QRadioButton official document)
Radio buttons are typically used to force the user to select one of several options. So in one widget, multiple radio buttons are set to autoExclusive by default. If you select one button, the other buttons are deselected.
Related course: Create Desktop Apps with Python PyQt5
QRadioButton.
If you want to be able to select multiple buttons at once, you can enter False in the setAutoExclusive() method. You can also use the QButtonGroup() method if you want to place multiple exclusive button groups within a widget. (See QButtonGroup official documentation)
As with the check box, a toggled() signal occurs when the button changes state. You can also use the isChecked() method when you want to get the state of a particular button.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QRadioButton
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
rbtn1 = QRadioButton('First Button', self)
rbtn1.move(50, 50)
rbtn1.setChecked(True)
rbtn2 = QRadioButton(self)
rbtn2.move(50, 70)
rbtn2.setText('Second Button')
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QRadioButton')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
I created two radio buttons.
Description
rbtn1 = QRadioButton('First Button', self)
Create one QRadioButton. Enter the text and parent widget that you want to enter in the label.
rbtn1.setChecked(True)
If setChecked() is set to True, the button is selected and displayed when the program is run.
rbtn2.setText('Second Button')
You can also set the text of the label through the setText() method.