PyQT QRadioButton
is a simple radio button. This is typically used when only one option is possible, as opposed to a checkbox.
In qt the checkbox always has the round button and the label like QRadioButton("Australia")
.
Related Course: Create GUI Apps with Python PyQt5
The code below creates 3 radio buttons. It adds 3 radio buttons to a grid. If you click on any of the radio buttons, it calls the method onClicked()
. The radio button is connected to that method using radiobutton.toggled.connect(self.onClicked)
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 from PyQt5.QtWidgets import *import sysclass Window (QWidget) : def __init__ (self) : QWidget.__init__(self) layout = QGridLayout() self.setLayout(layout) radiobutton = QRadioButton("Australia" ) radiobutton.setChecked(True ) radiobutton.country = "Australia" radiobutton.toggled.connect(self.onClicked) layout.addWidget(radiobutton, 0 , 0 ) radiobutton = QRadioButton("China" ) radiobutton.country = "China" radiobutton.toggled.connect(self.onClicked) layout.addWidget(radiobutton, 0 , 1 ) radiobutton = QRadioButton("Japan" ) radiobutton.country = "Japan" radiobutton.toggled.connect(self.onClicked) layout.addWidget(radiobutton, 0 , 2 ) def onClicked (self) : radioButton = self.sender() if radioButton.isChecked(): print("Country is %s" % (radioButton.country)) app = QApplication(sys.argv) screen = Window() screen.show() sys.exit(app.exec_())
If you are new to Python PyQt, then I highly recommend this book.
Download Examples