QCheckBox 复选按钮
QCheckBox 是 PyQt6 里的复选按钮元件,这篇教学会介绍如何在 PyQt6 视窗里加入 QCheckBox 复选按钮,并进行一些基本的样式设定,以及进行按钮群组和点击事件的设定。
快速导览:
因为 Google Colab 不支援 PyQt6,所以请使用本机环境 ( 参考:使用 Python 虚拟环境 ) 或使用 Anaconda Jupyter 进行实作 ( 参考:使用 Anaconda )。
加入 QCheckBox 复选按钮
建立 PyQt6 视窗对象后,透过 QtWidgets.QCheckBox(widget) 方法,就能在指定的元件中建立复选按钮,下方的程序码执行后,会加入三个 QCheckBox 按钮 ,并使用 setText() 方法加入文字。
from PyQt6 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
Form.setWindowTitle('oxxo.studio')
Form.resize(300, 200)
cb_a = QtWidgets.QCheckBox(Form) # 複選按鈕 A
cb_a.setGeometry(30, 60, 50, 20)
cb_a.setText('A')
cb_b = QtWidgets.QCheckBox(Form) # 複選按鈕 B
cb_b.setGeometry(80, 60, 50, 20)
cb_b.setText('B')
cb_c = QtWidgets.QCheckBox(Form) # 複選按鈕 C
cb_c.setGeometry(130, 60, 50, 20)
cb_c.setText('C')
Form.show()
sys.exit(app.exec())
class 写法:
from PyQt6 import QtWidgets
import sys
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('oxxo.studio')
self.resize(300, 200)
self.ui()
def ui(self):
self.cb_a = QtWidgets.QCheckBox(self) # 複選按鈕 A
self.cb_a.setGeometry(30, 60, 50, 20)
self.cb_a.setText('A')
self.cb_b = QtWidgets.QCheckBox(self) # 複選按鈕 B
self.cb_b.setGeometry(80, 60, 50, 20)
self.cb_b.setText('B')
self.cb_c = QtWidgets.QCheckBox(self) # 複選按鈕 C
self.cb_c.setGeometry(130, 60, 50, 20)
self.cb_c.setText('C')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
Form = MyWidget()
Form.show()
sys.exit(app.exec())
QCheckBox 位置设定
透过下列 QCheckBox 方法,可以将 QCheckBox 元件定位到指定的位置:
|方法|参数|说明| |--|--| |move()|x, y|设定 QCheckBox 在摆放的父元件中的 xy 座标,x 往右为正,y 往下为正,尺寸根据内容自动延伸。| |setGeometry()|x, y, w, h|设定 QCheckBox 在摆放的父元件中的 xy 座标和长宽尺寸,x 往右为正,y 往下为正,如果超过长宽尺寸,默认会被裁切无法显示。|
下方的程序码执行后会放入四个 QCheckBox,两个使用 move() 定位,另外两个使用 setGeometry() 方法定位。
from PyQt6 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
Form.setWindowTitle('oxxo.studio')
Form.resize(300, 200)
cb_a = QtWidgets.QCheckBox(Form) # 複選按鈕 A
cb_a.move(30, 60)
cb_a.setText('A')
cb_b = QtWidgets.QCheckBox(Form) # 複選按鈕 B
cb_b.move(80, 60)
cb_b.setText('B')
cb_c = QtWidgets.QCheckBox(Form) # 複選按鈕 C
cb_c.setGeometry(130, 60, 50, 20)
cb_c.setText('C')
cb_d = QtWidgets.QCheckBox(Form) # 複選按鈕 D
cb_d.setGeometry(180, 60, 50, 20)
cb_d.setText('D')
Form.show()
sys.exit(app.exec())
class 写法:
from PyQt6 import QtWidgets
import sys
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('oxxo.studio')
self.resize(300, 200)
self.ui()
def ui(self):
self.cb_a = QtWidgets.QCheckBox(self) # 複選按鈕 A
self.cb_a.move(30, 60)
self.cb_a.setText('A')
self.cb_b = QtWidgets.QCheckBox(self) # 複選按鈕 B
self.cb_b.move(80, 60)
self.cb_b.setText('B')
self.cb_c = QtWidgets.QCheckBox(self) # 複選按鈕 C
self.cb_c.setGeometry(130, 60, 50, 20)
self.cb_c.setText('C')
self.cb_d = QtWidgets.QCheckBox(self) # 複選按鈕 D
self.cb_d.setGeometry(180, 60, 50, 20)
self.cb_d.setText('D')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
Form = MyWidget()
Form.show()
sys.exit(app.exec())
QCheckBox 状态设定
透过下列几种方法,可以设定 QCheckBox 的状态:
| 方法 | 参数 | 说明 |
|---|---|---|
| setDisabled() | bool | 是否停用,默认 False 启用,可设定 True 停用。 |
| setChecked() | bool | 是否勾选,默认 False 不勾选,可设定 True 勾选,若同一组有多个勾选,则以最后一个为主。 |
| toggle() | 勾选状态切换。 |
下面的程序码执行后,QCheckBox B 会停用,QCheckBox A 会预先勾选。
from PyQt6 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
Form.setWindowTitle('oxxo.studio')
Form.resize(300, 200)
cb_a = QtWidgets.QCheckBox(Form) # 複選按鈕 A
cb_a.move(30, 60)
cb_a.setText('A')
cb_a.setChecked(True) # 預先選取
cb_b = QtWidgets.QCheckBox(Form) # 複選按鈕 B
cb_b.move(80, 60)
cb_b.setText('B')
cb_b.setDisabled(True) # 停用
cb_c = QtWidgets.QCheckBox(Form)
cb_c.setGeometry(130, 60, 50, 20)
cb_c.setText('C')
Form.show()
sys.exit(app.exec())
class 写法:
from PyQt6 import QtWidgets
import sys
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('oxxo.studio')
self.resize(300, 200)
self.ui()
def ui(self):
self.cb_a = QtWidgets.QCheckBox(self) # 複選按鈕 A
self.cb_a.move(30, 60)
self.cb_a.setText('A')
self.cb_a.setChecked(True) # 預先選取
self.cb_b = QtWidgets.QCheckBox(self) # 複選按鈕 B
self.cb_b.move(80, 60)
self.cb_b.setText('B')
self.cb_b.setDisabled(True) # 停用
self.cb_c = QtWidgets.QCheckBox(self)
self.cb_c.setGeometry(130, 60, 50, 20)
self.cb_c.setText('C')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
Form = MyWidget()
Form.show()
sys.exit(app.exec())
QCheckBox 样式设定
如果会使用网页 CSS 语法,就能透过 setStyleSheet() 设定 QCheckBox 样式,在设计样式上也较为弹性好用,下方的程序码执行后,会套用 CSS 样式语法,将 QCheckBox 设定为蓝色字,当鼠标移到按钮上,就会触发 hover 的样式而变成红色字,勾选之后就会变成黑底白字。
from PyQt6 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
Form.setWindowTitle('oxxo.studio')
Form.resize(300, 200)
# 設定 QCheckBox
style = '''
QCheckBox {
color: #00f;
}
QCheckBox:hover {
color: #f00;
}
QCheckBox:checked {
color: #fff;
background: #000;
}
'''
cb_a = QtWidgets.QCheckBox(Form)
cb_a.move(30, 60)
cb_a.setText('A')
cb_a.setStyleSheet(style) # 套用 style
cb_b = QtWidgets.QCheckBox(Form)
cb_b.move(80, 60)
cb_b.setText('B')
cb_b.setStyleSheet(style) # 套用 style
cb_c = QtWidgets.QCheckBox(Form)
cb_c.move(130, 60)
cb_c.setText('C')
cb_c.setStyleSheet(style) # 套用 style
Form.show()
sys.exit(app.exec())
class 写法:
from PyQt6 import QtWidgets
import sys
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('oxxo.studio')
self.resize(300, 200)
self.ui()
def ui(self):
# 設定 QCheckBox
style = '''
QCheckBox {
color: #00f;
}
QCheckBox:hover {
color: #f00;
}
QCheckBox:checked {
color: #fff;
background: #000;
}
'''
self.cb_a = QtWidgets.QCheckBox(self)
self.cb_a.move(30, 60)
self.cb_a.setText('A')
self.cb_a.setStyleSheet(style) # 套用 style
self.cb_b = QtWidgets.QCheckBox(self)
self.cb_b.move(80, 60)
self.cb_b.setText('B')
self.cb_b.setStyleSheet(style) # 套用 style
self.cb_c = QtWidgets.QCheckBox(self)
self.cb_c.move(130, 60)
self.cb_c.setText('C')
self.cb_c.setStyleSheet(style) # 套用 style
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
Form = MyWidget()
Form.show()
sys.exit(app.exec())
QCheckBox 点击事件
如果要侦测勾选了哪个 QCheckBox,可以使用 clicked() 的方法,将函数与各个按钮绑定,接着就能透过 text() 取得按钮文字,透过 isChecked() 取得按钮勾选状态,下方的程序码执行后,勾选 QCheckBox 时,就会将勾选的按钮文字组合,透过 QLabel 输出显示。
from PyQt6 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
Form.setWindowTitle('oxxo.studio')
Form.resize(300, 200)
arr = ['']*3 # 先新增一個串列放入文字
def show(cb, i):
global a
if cb.isChecked():
arr[i] = cb.text() # 如果該按鈕是勾選狀態,在串列的指定位置放入文字
else:
arr[i] = '' # 如果該按鈕是勾選狀態,在串列的指定位置放入空字串
output = ''.join(arr) # 組合串列內容為文字
label.setText(output) # label 顯示文字
label = QtWidgets.QLabel(Form)
label.setGeometry(30, 30, 100, 30)
cb_a = QtWidgets.QCheckBox(Form)
cb_a.move(30, 60)
cb_a.setText('A')
cb_a.clicked.connect(lambda:show(cb_a, 0)) # 點擊按鈕時,回傳兩個參數給 show 函式
cb_b = QtWidgets.QCheckBox(Form)
cb_b.move(80, 60)
cb_b.setText('B')
cb_b.clicked.connect(lambda:show(cb_b, 1)) # 點擊按鈕時,回傳兩個參數給 show 函式
cb_c = QtWidgets.QCheckBox(Form)
cb_c.move(130, 60)
cb_c.setText('C')
cb_c.clicked.connect(lambda:show(cb_c, 2)) # 點擊按鈕時,回傳兩個參數給 show 函式
Form.show()
sys.exit(app.exec())
class 写法 ( 注意不能使用 show 作为方法名称,会覆写基底的 show 方法造成无法显示 ):
from PyQt6 import QtWidgets
import sys
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('oxxo.studio')
self.resize(300, 200)
self.ui()
def ui(self):
self.arr = ['']*3 # 先新增一個串列放入文字
self.label = QtWidgets.QLabel(self)
self.label.setGeometry(30, 30, 100, 30)
self.cb_a = QtWidgets.QCheckBox(self)
self.cb_a.move(30, 60)
self.cb_a.setText('A')
self.cb_a.clicked.connect(lambda:self.showText(self.cb_a, 0)) # 點擊按鈕時,回傳兩個參數給 show 函式
self.cb_b = QtWidgets.QCheckBox(self)
self.cb_b.move(80, 60)
self.cb_b.setText('B')
self.cb_b.clicked.connect(lambda:self.showText(self.cb_b, 1)) # 點擊按鈕時,回傳兩個參數給 show 函式
self.cb_c = QtWidgets.QCheckBox(self)
self.cb_c.move(130, 60)
self.cb_c.setText('C')
self.cb_c.clicked.connect(lambda:self.showText(self.cb_c, 2)) # 點擊按鈕時,回傳兩個參數給 show 函式
def showText(self, cb, i):
if cb.isChecked():
self.arr[i] = cb.text() # 如果該按鈕是勾選狀態,在串列的指定位置放入文字
else:
self.arr[i] = '' # 如果該按鈕是勾選狀態,在串列的指定位置放入空字串
output = ''.join(self.arr) # 組合串列內容為文字
self.label.setText(output) # label 顯示文字
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
Form = MyWidget()
Form.show()
sys.exit(app.exec())
微信扫码关注
抖音扫码关注