最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

QCheckBox 復(fù)選按鈕

2023-03-22 07:18 作者:千牛不是牛  | 我要投稿

QCheckBox 復(fù)選按鈕

QCheckBox 是 PyQt6 里的復(fù)選按鈕控件,這篇教學(xué)會介紹如何在 PyQt6 視窗里加入 QCheckBox 復(fù)選按鈕,并進(jìn)行一些基本的樣式設(shè)定,以及進(jìn)行按鈕群組和點擊事件的設(shè)定。

快速預(yù)覽:

  • 加入 QCheckBox 復(fù)選按鈕

  • QCheckBox位置設(shè)置

  • QCheckBox 狀態(tài)設(shè)定

  • QCheckBox 樣式設(shè)定

  • QCheckBox 點擊事件

加入 QCheckBox 復(fù)選按鈕

建立 PyQt6 視窗物件后,透過 QtWidgets.QCheckBox(widget)方法,就能在指定的控件中建立復(fù)選按鈕,下方的程式碼執(zhí)行后,會加入三個 QCheckBox 按鈕 ,并使用 setText() 方法加入文字。

?from PyQt6 import QtWidgets
?import sys
?app = QtWidgets.QApplication(sys.argv)
?
?Form = QtWidgets.QWidget()
?Form.setWindowTitle('千牛編程思維')
?Form.resize(300, 200)
?
?cb_a = QtWidgets.QCheckBox(Form) ? ?# 復(fù)選按鈕 A
?cb_a.setGeometry(30, 60, 50, 20)
?cb_a.setText('A')
?
?cb_b = QtWidgets.QCheckBox(Form) ? ?# 復(fù)選按鈕 B
?cb_b.setGeometry(80, 60, 50, 20)
?cb_b.setText('B')
?
?cb_c = QtWidgets.QCheckBox(Form) ? ?# 復(fù)選按鈕 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('千牛編程思維')
? ? ? ? ?self.resize(300, 200)
? ? ? ? ?self.ui()
?
? ? ?def ui(self):
? ? ? ? ?self.cb_a = QtWidgets.QCheckBox(self) ? ?# 復(fù)選按鈕 A
? ? ? ? ?self.cb_a.setGeometry(30, 60, 50, 20)
? ? ? ? ?self.cb_a.setText('A')
?
? ? ? ? ?self.cb_b = QtWidgets.QCheckBox(self) ? ?# 復(fù)選按鈕 B
? ? ? ? ?self.cb_b.setGeometry(80, 60, 50, 20)
? ? ? ? ?self.cb_b.setText('B')
?
? ? ? ? ?self.cb_c = QtWidgets.QCheckBox(self) ? ?# 復(fù)選按鈕 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())

image-20230321135510752

QCheckBox位置設(shè)置

透過下列 QCheckBox 方法,可以將 QCheckBox 控件定位到指定的位置:

方法參數(shù)說明移動x, y設(shè)定 QCheckBox 在擺放的父控件中的 xy 坐標(biāo),x 往右為正,y 往下為正,尺寸根據(jù)內(nèi)容自動延伸。設(shè)置幾何()X,X,Y,W,H設(shè)定 QCheckBox 在擺放的父控件中的 xy 坐標(biāo)和長寬尺寸,x 往右為正,y 往下為正,如果超過長寬尺寸,預(yù)設(shè)會被裁切無法顯示。

下方的程式碼執(zhí)行后會放入四個 QCheckBox,兩個使用 move() 定位,另外兩個使用 setGeometry() 方法定位。

?from PyQt6 import QtWidgets
?import sys
?app = QtWidgets.QApplication(sys.argv)
?
?Form = QtWidgets.QWidget()
?Form.setWindowTitle('千牛編程思維')
?Form.resize(300, 200)
?
?cb_a = QtWidgets.QCheckBox(Form) ? ?# 復(fù)選按鈕 A
?cb_a.move(30, 60)
?cb_a.setText('A')
?
?cb_b = QtWidgets.QCheckBox(Form) ? ?# 復(fù)選按鈕 B
?cb_b.move(80, 60)
?cb_b.setText('B')
?
?cb_c = QtWidgets.QCheckBox(Form) ? ?# 復(fù)選按鈕 C
?cb_c.setGeometry(130, 60, 50, 20)
?cb_c.setText('C')
?
?cb_d = QtWidgets.QCheckBox(Form) ? ?# 復(fù)選按鈕 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('千牛編程思維')
? ? ? ? ?self.resize(300, 200)
? ? ? ? ?self.ui()
?
? ? ?def ui(self):
? ? ? ? ?self.cb_a = QtWidgets.QCheckBox(self) ? ?# 復(fù)選按鈕 A
? ? ? ? ?self.cb_a.move(30, 60)
? ? ? ? ?self.cb_a.setText('A')
?
? ? ? ? ?self.cb_b = QtWidgets.QCheckBox(self) ? ?# 復(fù)選按鈕 B
? ? ? ? ?self.cb_b.move(80, 60)
? ? ? ? ?self.cb_b.setText('B')
?
? ? ? ? ?self.cb_c = QtWidgets.QCheckBox(self) ? ?# 復(fù)選按鈕 C
? ? ? ? ?self.cb_c.setGeometry(130, 60, 50, 20)
? ? ? ? ?self.cb_c.setText('C')
?
? ? ? ? ?self.cb_d = QtWidgets.QCheckBox(self) ? ?# 復(fù)選按鈕 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())

image-20230321135551037

QCheckBox 狀態(tài)設(shè)定

透過下列幾種方法,可以設(shè)定 QCheckBox 的狀態(tài):

方法參數(shù)說明設(shè)置已禁用()布爾是否停用,預(yù)設(shè) False 啟用,可設(shè)定 True 停用。已檢查過()布爾是否勾選,預(yù)設(shè) False 不勾選,可設(shè)定 True 勾選,若同一組有多個勾選,則以最后一個為主。切換開關(guān)勾選狀態(tài)切換。

下面的程式碼執(zhí)行后,QCheckBox B 會停用,QCheckBox A 會預(yù)先勾選。

?from PyQt6 import QtWidgets
?import sys
?app = QtWidgets.QApplication(sys.argv)
?
?Form = QtWidgets.QWidget()
?Form.setWindowTitle('千牛編程思維')
?Form.resize(300, 200)
?
?cb_a = QtWidgets.QCheckBox(Form) ? ?# 復(fù)選按鈕 A
?cb_a.move(30, 60)
?cb_a.setText('A')
?cb_a.setChecked(True) ? ? ? ? ? ? ? # 預(yù)設(shè)選取
?
?cb_b = QtWidgets.QCheckBox(Form) ? ?# 復(fù)選按鈕 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('千牛編程思維')
? ? ? ? ?self.resize(300, 200)
? ? ? ? ?self.ui()
?
? ? ?def ui(self):
? ? ? ? ?self.cb_a = QtWidgets.QCheckBox(self) ? ?# 復(fù)選按鈕 A
? ? ? ? ?self.cb_a.move(30, 60)
? ? ? ? ?self.cb_a.setText('A')
? ? ? ? ?self.cb_a.setChecked(True) ? ? ? ? ? ? ? # 預(yù)設(shè)選取
?
? ? ? ? ?self.cb_b = QtWidgets.QCheckBox(self) ? ?# 復(fù)選按鈕 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())

image-20230321135632615

QCheckBox 樣式設(shè)定

如果會使用網(wǎng)頁 CSS 語法,就能透過 setStyleSheet() 設(shè)定 QCheckBox 樣式,在設(shè)計樣式上也較為彈性好用,下方的程式碼執(zhí)行后,會套用 CSS 樣式語法,將 QCheckBox 設(shè)定為藍(lán)色字,當(dāng)滑鼠移到按鈕上,就會觸發(fā) hover 的樣式而變成紅色字,勾選之后就會變成黑底白字。

?from PyQt6 import QtWidgets
?import sys
?app = QtWidgets.QApplication(sys.argv)
?
?Form = QtWidgets.QWidget()
?Form.setWindowTitle('千牛編程思維')
?Form.resize(300, 200)
?
?# 設(shè)定 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('千牛編程思維')
? ? ? ? ?self.resize(300, 200)
? ? ? ? ?self.ui()
?
? ? ?def ui(self):
? ? ? ? ?# 設(shè)定 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())

image-20230321135703877

QCheckBox 點擊事件

如果要偵測勾選了哪個 QCheckBox,可以使用 clicked() 的方法,將函式與各個按鈕綁定,接著就能透過 text() 取得按鈕文字,透過 isChecked() 取得按鈕勾選狀態(tài),下方的程式碼執(zhí)行后,勾選 QCheckBox 時,就會將勾選的按鈕文字組合,透過 QLabel 輸出顯示。

from PyQt6 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)

Form = QtWidgets.QWidget()
Form.setWindowTitle('千牛編程思維')
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 = 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)) ?

cb_b = QtWidgets.QCheckBox(Form)
cb_b.move(80, 60)
cb_b.setText('B')
cb_b.clicked.connect(lambda:show(cb_b, 1)) ?

cb_c = QtWidgets.QCheckBox(Form)
cb_c.move(130, 60)
cb_c.setText('C')
cb_c.clicked.connect(lambda:show(cb_c, 2))

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('千牛編程思維')
? ? ? ?self.resize(300, 200)
? ? ? ?self.ui()

? ?def ui(self):
? ? ? ?self.arr = ['']*3 ? ? ? ? ? ? ? ? ? # 定義一個長度為3的空列表

? ? ? ?self.label = QtWidgets.QLabel(self) ?# 創(chuàng)建一個QLabel控件
? ? ? ?self.label.setGeometry(30, 30, 100, 30) ?# 設(shè)置QLabel控件的位置和大小

? ? ? ?self.cb_a = QtWidgets.QCheckBox(self) ?# 創(chuàng)建一個QCheckBox控件
? ? ? ?self.cb_a.move(30, 60) ? ? ? ? ? ? ? ?# 設(shè)置QCheckBox控件的位置
? ? ? ?self.cb_a.setText('A') ? ? ? ? ? ? ? ?# 設(shè)置QCheckBox控件的文本
? ? ? ?self.cb_a.clicked.connect(lambda:self.showText(self.cb_a, 0)) ?

? ? ? ?self.cb_b = QtWidgets.QCheckBox(self) ?# 創(chuàng)建一個QCheckBox控件
? ? ? ?self.cb_b.move(80, 60) ? ? ? ? ? ? ? ?# 設(shè)置QCheckBox控件的位置
? ? ? ?self.cb_b.setText('B') ? ? ? ? ? ? ? ?# 設(shè)置QCheckBox控件的文本
? ? ? ?self.cb_b.clicked.connect(lambda:self.showText(self.cb_b, 1)) ?

? ? ? ?self.cb_c = QtWidgets.QCheckBox(self) ?# 創(chuàng)建一個QCheckBox控件
? ? ? ?self.cb_c.move(130, 60) ? ? ? ? ? ? ? # 設(shè)置QCheckBox控件的位置
? ? ? ?self.cb_c.setText('C') ? ? ? ? ? ? ? ?# 設(shè)置QCheckBox控件的文本
? ? ? ?self.cb_c.clicked.connect(lambda:self.showText(self.cb_c, 2)) ?
? ? ? ?
? ?# 定義一個showText方法,用于處理QCheckBox控件的點擊事件
? ?def showText(self, cb, i):
? ? ? ?if cb.isChecked(): ? ? ? ? ? ? ? ?# 如果QCheckBox控件被選中
? ? ? ? ? ?self.arr[i] = cb.text() ? ? ? # 將對應(yīng)的文本添加到列表中
? ? ? ?else:
? ? ? ? ? ?self.arr[i] = '' ? ? ? ? ? ? ?# 如果QCheckBox控件被取消選中,則將對應(yīng)的列表元素置為空字符串
? ? ? ?output = ''.join(self.arr) ? ? ? ? # 將列表中的元素合并為一個字符串
? ? ? ?self.label.setText(output) ? ? ? ? # 將合并后的字符串設(shè)置為QLabel控件的文本 ?

if __name__ == '__main__':
? ?app = QtWidgets.QApplication(sys.argv)
? ?Form = MyWidget()
? ?Form.show()
? ?sys.exit(app.exec())



QCheckBox 復(fù)選按鈕的評論 (共 條)

分享到微博請遵守國家法律
慈溪市| 容城县| 陇西县| 巴里| 甘德县| 六安市| 自贡市| 怀仁县| 文登市| 同德县| SHOW| 天镇县| 长泰县| 丰都县| 普安县| 应用必备| 北票市| 东乌珠穆沁旗| 台江县| 永平县| 洛隆县| 日土县| 咸阳市| 仲巴县| 江口县| 涪陵区| 南丹县| 寿光市| 松阳县| 嘉荫县| 宁晋县| 古浪县| 武隆县| 内江市| 驻马店市| 海宁市| 太仆寺旗| 晋城| 随州市| 石楼县| 盐源县|