本篇 ShengYu 介紹 Python PyQt5 QRadioButton 單選框用法與範例,QRadioButton 可以作一些多選項擇一的選取功能,例如性別選取、葷素食選取等等。
以下的 Python PyQt5 QRadioButton 用法與範例將分為這幾部分,
- PyQt5 QRadioButton 基本用法
- PyQt5 QRadioButton 多組選取
- PyQt5 設定 QRadioButton 預設的選項
- PyQt5 QRadioButton 綁定事件
那我們開始吧!
這邊介紹 PyQt5 建立 QRadioButton 的基本用法,QRadioButton 基本上也是個按鈕,在建構 QRadioButton 時帶入顯示的文字,多個 QRadioButton 在同一個父類視窗下是互斥的,也就是在同一個父類視窗下的多個 QRadioButton 只能一個選取一個,如果選取另一個 QRadioButton 的話,先前選取的 QRadioButton 則會被取消,QRadioButton 搭配 QButtonGroup 使用的話則可以實做出多群組 QRadioButton 的互斥選取,稍後會介紹到。
以下示範將三個 QRadioButton 放入同一個父類裡,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#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout,
QRadioButton)
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)
layout = QVBoxLayout()
self.setLayout(layout)
self.myradiobutton1 = QRadioButton('apple', self)
layout.addWidget(self.myradiobutton1)
self.myradiobutton2 = QRadioButton('banana', self)
layout.addWidget(self.myradiobutton2)
self.myradiobutton3 = QRadioButton('orange', self)
layout.addWidget(self.myradiobutton3)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
結果圖如下,所以實際操作時可以發現同一群裡的 QRadioButton 單選框是互斥的,只能選擇其中一個 QRadioButton,
那如果想要產生 QRadioButton 多組選取呢?在下節範例馬上為你介紹。
PyQt5 如果想要產生 QRadioButton 多組選取要怎麼實作呢?
這時候可以使用 QButtonGroup,這邊示範兩群組 QRadioButton 選取,將二個群組的 QRadioButton 各放入二個 QButtonGroup,這邊我們建立二個 QButtonGroup,把 myradiobutton1 ~ myradiobutton3 放入第一個 QButtonGroup,把 myradiobutton4 ~ myradiobutton6 放入第二個 QButtonGroup,同時也把 layout 改成 QGridLayout,左邊為第一群,右邊為第二群,方便示範說明,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#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout,
QRadioButton, QButtonGroup)
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)
layout = QGridLayout()
self.setLayout(layout)
self.myradiobutton1 = QRadioButton('apple', self)
layout.addWidget(self.myradiobutton1, 0, 0)
self.myradiobutton2 = QRadioButton('banana', self)
layout.addWidget(self.myradiobutton2, 1, 0)
self.myradiobutton3 = QRadioButton('orange', self)
layout.addWidget(self.myradiobutton3, 2, 0)
self.myradiobutton4 = QRadioButton('lemon', self)
layout.addWidget(self.myradiobutton4, 0, 1)
self.myradiobutton5 = QRadioButton('strawberry', self)
layout.addWidget(self.myradiobutton5, 1, 1)
self.myradiobutton6 = QRadioButton('tomato', self)
layout.addWidget(self.myradiobutton6, 2, 1)
self.buttongroup = QButtonGroup(self)
self.buttongroup.addButton(self.myradiobutton1, 1)
self.buttongroup.addButton(self.myradiobutton2, 2)
self.buttongroup.addButton(self.myradiobutton3, 3)
self.buttongroup2 = QButtonGroup(self)
self.buttongroup2.addButton(self.myradiobutton4, 4)
self.buttongroup2.addButton(self.myradiobutton5, 5)
self.buttongroup2.addButton(self.myradiobutton6, 6)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
結果圖如下,
這邊介紹 PyQt5 設定 QCheckBox 預設的選項,建立 QCheckBox 後預設是不勾選的狀態,有兩種方式可以改變這個狀態,
一個是使用 QCheckBox.toggle()
,toggle 就是原本勾選的會變成不勾選,在使用一次 toggle 的話原本不勾選的會變成勾選,
另一個方式是透過 QCheckBox.setChecked()
來設定 CheckBox 是否要勾選,True 為 勾選,False 為不勾選,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#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout,
QRadioButton, QButtonGroup)
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)
layout = QGridLayout()
self.setLayout(layout)
self.myradiobutton1 = QRadioButton('apple', self)
layout.addWidget(self.myradiobutton1, 0, 0)
self.myradiobutton2 = QRadioButton('banana', self)
layout.addWidget(self.myradiobutton2, 1, 0)
self.myradiobutton3 = QRadioButton('orange', self)
layout.addWidget(self.myradiobutton3, 2, 0)
self.myradiobutton4 = QRadioButton('lemon', self)
layout.addWidget(self.myradiobutton4, 0, 1)
self.myradiobutton5 = QRadioButton('strawberry', self)
layout.addWidget(self.myradiobutton5, 1, 1)
self.myradiobutton6 = QRadioButton('tomato', self)
layout.addWidget(self.myradiobutton6, 2, 1)
self.buttongroup = QButtonGroup(self)
self.buttongroup.addButton(self.myradiobutton1, 1)
self.buttongroup.addButton(self.myradiobutton2, 2)
self.buttongroup.addButton(self.myradiobutton3, 3)
self.buttongroup2 = QButtonGroup(self)
self.buttongroup2.addButton(self.myradiobutton4, 4)
self.buttongroup2.addButton(self.myradiobutton5, 5)
self.buttongroup2.addButton(self.myradiobutton6, 6)
self.myradiobutton1.toggle()
self.myradiobutton4.setChecked(True)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
結果圖如下,
這邊介紹 PyQt5 QRadioButton 如何綁定事件,透過先前的範例我們將 QRadioButton 加入 QButtonGroup 後,所以這邊我們是要將 QButtonGroup 的 buttonClicked 事件連接到我們要處理的函式,第一群連結至 onButtonGroup1Click()
,第二群連結至 onButtonGroup2Click()
,範例如下,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
76
77
78
79
80
81
82
83
84
85
86
87#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout,
QRadioButton, QButtonGroup, QPushButton)
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)
layout = QGridLayout()
self.setLayout(layout)
self.myradiobutton1 = QRadioButton('apple', self)
layout.addWidget(self.myradiobutton1, 0, 0)
self.myradiobutton2 = QRadioButton('banana', self)
layout.addWidget(self.myradiobutton2, 1, 0)
self.myradiobutton3 = QRadioButton('orange', self)
layout.addWidget(self.myradiobutton3, 2, 0)
self.myradiobutton4 = QRadioButton('lemon', self)
layout.addWidget(self.myradiobutton4, 0, 1)
self.myradiobutton5 = QRadioButton('strawberry', self)
layout.addWidget(self.myradiobutton5, 1, 1)
self.myradiobutton6 = QRadioButton('tomato', self)
layout.addWidget(self.myradiobutton6, 2, 1)
self.mybutton = QPushButton('button', self)
layout.addWidget(self.mybutton, 3, 1)
self.buttongroup1 = QButtonGroup(self)
self.buttongroup1.addButton(self.myradiobutton1, 1)
self.buttongroup1.addButton(self.myradiobutton2, 2)
self.buttongroup1.addButton(self.myradiobutton3, 3)
self.buttongroup2 = QButtonGroup(self)
self.buttongroup2.addButton(self.myradiobutton4, 4)
self.buttongroup2.addButton(self.myradiobutton5, 5)
self.buttongroup2.addButton(self.myradiobutton6, 6)
self.buttongroup1.buttonClicked.connect(self.onButtonGroup1Click)
self.buttongroup2.buttonClicked.connect(self.onButtonGroup2Click)
self.mybutton.clicked.connect(self.onButtonClick)
self.myradiobutton1.setChecked(True)
self.myradiobutton4.setChecked(True)
self.fruit1 = self.myradiobutton1.text()
self.fruit2 = self.myradiobutton4.text()
def onButtonGroup1Click(self):
if self.buttongroup1.checkedId() == 1:
print('1 checked')
self.fruit1 = self.myradiobutton1.text()
elif self.buttongroup1.checkedId() == 2:
print('2 checked')
self.fruit1 = self.myradiobutton2.text()
elif self.buttongroup1.checkedId() == 3:
print('3 checked')
self.fruit1 = self.myradiobutton3.text()
else:
print('? checked')
def onButtonGroup2Click(self):
if self.buttongroup2.checkedId() == 4:
print('4 checked')
self.fruit2 = self.myradiobutton4.text()
elif self.buttongroup2.checkedId() == 5:
print('5 checked')
self.fruit2 = self.myradiobutton5.text()
elif self.buttongroup2.checkedId() == 6:
print('6 checked')
self.fruit2 = self.myradiobutton6.text()
else:
print('? checked')
def onButtonClick(self):
print(self.fruit1 + ' + ' + self.fruit2)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
結果圖如下,1
2
32 checked
6 checked
banana + tomato
也可以將兩個 QButtonGroup 的 buttonClicked 事件都連結到 onButtonGroupClick()
同一個函式處理,在 onButtonGroupClick()
裡使用 self.sender()
來判斷是哪一個 QButtonGroup,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
76
77
78
79
80
81
82
83
84
85
86
87
88#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout,
QRadioButton, QButtonGroup, QPushButton)
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('my window')
self.setGeometry(50, 50, 200, 150)
layout = QGridLayout()
self.setLayout(layout)
self.myradiobutton1 = QRadioButton('apple', self)
layout.addWidget(self.myradiobutton1, 0, 0)
self.myradiobutton2 = QRadioButton('banana', self)
layout.addWidget(self.myradiobutton2, 1, 0)
self.myradiobutton3 = QRadioButton('orange', self)
layout.addWidget(self.myradiobutton3, 2, 0)
self.myradiobutton4 = QRadioButton('lemon', self)
layout.addWidget(self.myradiobutton4, 0, 1)
self.myradiobutton5 = QRadioButton('strawberry', self)
layout.addWidget(self.myradiobutton5, 1, 1)
self.myradiobutton6 = QRadioButton('tomato', self)
layout.addWidget(self.myradiobutton6, 2, 1)
self.mybutton = QPushButton('button', self)
layout.addWidget(self.mybutton, 3, 1)
self.buttongroup1 = QButtonGroup(self)
self.buttongroup1.addButton(self.myradiobutton1, 1)
self.buttongroup1.addButton(self.myradiobutton2, 2)
self.buttongroup1.addButton(self.myradiobutton3, 3)
self.buttongroup2 = QButtonGroup(self)
self.buttongroup2.addButton(self.myradiobutton4, 4)
self.buttongroup2.addButton(self.myradiobutton5, 5)
self.buttongroup2.addButton(self.myradiobutton6, 6)
self.buttongroup1.buttonClicked.connect(self.onButtonGroupClick)
self.buttongroup2.buttonClicked.connect(self.onButtonGroupClick)
self.mybutton.clicked.connect(self.onButtonClick)
self.myradiobutton1.setChecked(True)
self.myradiobutton4.setChecked(True)
self.fruit1 = self.myradiobutton1.text()
self.fruit2 = self.myradiobutton4.text()
def onButtonGroupClick(self):
sender = self.sender()
if sender == self.buttongroup1:
if self.buttongroup1.checkedId() == 1:
print('1 checked')
self.fruit1 = self.myradiobutton1.text()
elif self.buttongroup1.checkedId() == 2:
print('2 checked')
self.fruit1 = self.myradiobutton2.text()
elif self.buttongroup1.checkedId() == 3:
print('3 checked')
self.fruit1 = self.myradiobutton3.text()
else:
print('? checked')
else:
if self.buttongroup2.checkedId() == 4:
print('4 checked')
self.fruit2 = self.myradiobutton4.text()
elif self.buttongroup2.checkedId() == 5:
print('5 checked')
self.fruit2 = self.myradiobutton5.text()
elif self.buttongroup2.checkedId() == 6:
print('6 checked')
self.fruit2 = self.myradiobutton6.text()
else:
print('? checked')
def onButtonClick(self):
print(self.fruit1 + ' + ' + self.fruit2)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
w.show()
sys.exit(app.exec_())
以上就是 Python PyQt5 QRadioButton 單選框用法與範例介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!
下一篇將會介紹 PyQt5 QCheckBox 複選框用法與範例