Skip to content

Instantly share code, notes, and snippets.

@Orizzu
Last active August 15, 2021 07:25
Show Gist options
  • Save Orizzu/fbbd473becb6d7f1a598c12915f37c52 to your computer and use it in GitHub Desktop.
Save Orizzu/fbbd473becb6d7f1a598c12915f37c52 to your computer and use it in GitHub Desktop.
QRadioButton PyQt5
# you can copy paste and run this code for test
# By default QRadioButtons can be selected at a time
import sys
from PyQt5.QtWidgets import QApplication, QHBoxLayout, QWidget, QMainWindow, QRadioButton
class Radiodemo(QWidget):
def __init__(self, parent=None):
super(Radiodemo, self).__init__(parent)
layout = QHBoxLayout()
self.b1=QRadioButton("Button1") # create
self.b1.setChecked(True) # set State (True/False)
self.b1.toggled.connect(lambda:self.myMethod(self.b1)) # connect our signal
self.b2=QRadioButton("Button2") # again create
self.b2.toggled.connect(lambda:self.myMethod(self.b2)) # and connect our signal
layout.addWidget(self.b1)
layout.addWidget(self.b2)
self.setLayout(layout)
self.setWindowTitle("RadioButton demo")
def myMethod(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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment