qradiobutton qt5 python

Solutions on MaxInterview for qradiobutton qt5 python by the best coders in the world

showing results for - "qradiobutton qt5 python"
Facundo
24 Jul 2017
1 pythonCopyimport sys
2from PyQt5.QtWidgets import (QLabel, QRadioButton, 
3                             QPushButton, QVBoxLayout, 
4                             QApplication, QWidget, 
5                             QButtonGroup)
6
7
8class basicRadiobuttonExample(QWidget):
9
10    def __init__(self):
11        super().__init__()
12
13        self.init_ui()
14
15    def init_ui(self):
16        self.label = QLabel('Which city do you live in?')
17        self.rbtn1 = QRadioButton('New York')
18        self.rbtn2 = QRadioButton('Houston')
19        self.label2 = QLabel("")
20        
21        self.label3 = QLabel('Which state do you live in?')
22        self.rbtn3 = QRadioButton('New York')
23        self.rbtn4 = QRadioButton('Texas')
24        self.label4 = QLabel("")
25        
26        self.btngroup1 = QButtonGroup()
27        self.btngroup2 = QButtonGroup()
28        
29        self.btngroup1.addButton(self.rbtn1)
30        self.btngroup1.addButton(self.rbtn2)
31        self.btngroup2.addButton(self.rbtn3)
32        self.btngroup2.addButton(self.rbtn4)
33        
34        self.rbtn1.toggled.connect(self.onClickedCity)
35        self.rbtn2.toggled.connect(self.onClickedCity)
36        
37        self.rbtn3.toggled.connect(self.onClickedState)
38        self.rbtn4.toggled.connect(self.onClickedState)        
39        
40
41        layout = QVBoxLayout()
42        layout.addWidget(self.label)
43        layout.addWidget(self.rbtn1)
44        layout.addWidget(self.rbtn2)
45        layout.addWidget(self.label2)
46        
47        layout.addWidget(self.label3)
48        layout.addWidget(self.rbtn3)
49        layout.addWidget(self.rbtn4)
50        layout.addWidget(self.label4)
51        
52        self.setGeometry(200, 200, 300, 300)
53
54        self.setLayout(layout)
55        self.setWindowTitle('PyQt5 Radio Button Example')        
56
57        self.show()
58
59    def onClickedCity(self):
60        radioBtn = self.sender()
61        if radioBtn.isChecked():
62            self.label2.setText("You live in " + radioBtn.text())
63            
64    def onClickedState(self):
65        radioBtn = self.sender()
66        if radioBtn.isChecked():
67            self.label4.setText("You live in " + radioBtn.text())
68
69
70if __name__ == '__main__':    
71    app = QApplication(sys.argv)
72    ex = basicRadiobuttonExample()
73    sys.exit(app.exec_())
74