-
Notifications
You must be signed in to change notification settings - Fork 3
/
09_radio_button.py
75 lines (56 loc) · 2.57 KB
/
09_radio_button.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QApplication, QDialog, QGroupBox, QVBoxLayout, QHBoxLayout, QRadioButton, QLabel
class MainWindow(QDialog):
def __init__(self):
super().__init__()
self.title = "PyQt5 Radio Button"
self.top = 400
self.left = 200
self.width = 400
self.height = 150
self.icon_name = "images/home.png"
self._init_window()
def _init_window(self):
self.setWindowIcon(QtGui.QIcon(self.icon_name))
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self._create_ui_components()
vbox = QVBoxLayout()
vbox.addWidget(self.group_box)
self.lbl_Info = QLabel(self)
self.lbl_Info.setFont(QtGui.QFont("Sanserif", 15))
vbox.addWidget(self.lbl_Info)
self.setLayout(vbox)
self.show()
def _create_ui_components(self):
self.group_box = QGroupBox("What is your favorite sport?")
self.group_box.setFont(QtGui.QFont("Sanserif", 13))
hbox_layout = QHBoxLayout()
self.rdbtn_soccer = QRadioButton("Soccer")
self.rdbtn_soccer.setIcon(QtGui.QIcon("images/soccer.png"))
self.rdbtn_soccer.setIconSize(QtCore.QSize(30, 30))
self.rdbtn_soccer.setFont(QtGui.QFont("Sanserif", 13))
self.rdbtn_soccer.toggled.connect(self._on_radiobutton_checked)
hbox_layout.addWidget(self.rdbtn_soccer)
self.rdbtn_basketball = QRadioButton("Basketball")
self.rdbtn_basketball.setIcon(QtGui.QIcon("images/basketball.png"))
self.rdbtn_basketball.setIconSize(QtCore.QSize(30, 30))
self.rdbtn_basketball.setFont(QtGui.QFont("Sanserif", 13))
self.rdbtn_basketball.toggled.connect(self._on_radiobutton_checked)
hbox_layout.addWidget(self.rdbtn_basketball)
self.rdbtn_tennis = QRadioButton("Tennis")
self.rdbtn_tennis.setIcon(QtGui.QIcon("images/tennis.png"))
self.rdbtn_tennis.setIconSize(QtCore.QSize(30, 30))
self.rdbtn_tennis.setFont(QtGui.QFont("Sanserif", 13))
self.rdbtn_tennis.toggled.connect(self._on_radiobutton_checked)
hbox_layout.addWidget(self.rdbtn_tennis)
self.group_box.setLayout(hbox_layout)
def _on_radiobutton_checked(self):
rdbtn = self.sender()
if rdbtn.isChecked():
self.lbl_Info.setText(f"Sport selected: {rdbtn.text()}")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec())