[PyQt6] PartⅡ. Creating QCheckBox

QCheckBox

关键方法:[].stateChanged
self.check1.stateChanged.connect(self.item_selected) # 如果状态改变,则传递信号

from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QRadioButton, QLabel, QVBoxLayout, QCheckBox
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()

        self.setGeometry(200, 200, 700, 400) # 设置窗口大小
        self.setWindowTitle("Python QCheckBox")
        self.setWindowIcon(QIcon('python.png')) # 设置图片,没有的话不显示

        hbox = QHBoxLayout()
        vbox = QVBoxLayout()

        self.check1 = QCheckBox("python")
        self.check1.setIcon(QIcon("sid.jpg"))
        self.check1.setIconSize(QSize(40, 40))
        self.check1.setFont(QFont("Sanserif", 13))
        self.check1.stateChanged.connect(self.item_selected) # 如果状态改变,则传递信号

        self.check2 = QCheckBox("Java")
        self.check2.setIcon(QIcon("python.png"))
        self.check2.setIconSize(QSize(40, 40))
        self.check2.setFont(QFont("Sanserif", 13))
        self.check2.stateChanged.connect(self.item_selected)

        self.check3 = QCheckBox("JS")
        self.check3.setIcon(QIcon("js.jpg"))
        self.check3.setIconSize(QSize(40, 40))
        self.check3.setFont(QFont("Sanserif", 13))
        self.check3.stateChanged.connect(self.item_selected)

        self.label = QLabel("Hello")
        self.label.setFont(QFont("Sanserif", 15))

        hbox.addWidget(self.check1)
        hbox.addWidget(self.check2)
        hbox.addWidget(self.check3)

        vbox.addWidget(self.label)

        vbox.addLayout(hbox)

        self.setLayout(vbox)

    def item_selected(self):
        value = ""

        if self.check1.isChecked():
            value = self.check1.text()
        if self.check2.isChecked():
            value = self.check2.text()
        if self.check3.isChecked():
            value = self.check3.text()

        self.label.setText("You have selected: " + value)

app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())


利用designer设计

关键代码

    self.checkBox.stateChanged.connect(self.check_selected)
    
    def check_selected(self):
        price = 10

        if self.checkBox.isChecked():
            price = price + 3

        if self.checkBox_2.isChecked():
            price = price + 4

        if self.checkBox_3.isChecked():
            price = price + 5

        self.label_3.setText(f"Total Price Is: {price}")

完整代码

# Form implementation generated from reading ui file 'CheckBox.ui'
#
# Created by: PyQt6 UI code generator 6.3.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt6 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 302)
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(Dialog)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setObjectName("label")
        self.verticalLayout_2.addWidget(self.label)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout.addWidget(self.label_2)
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.checkBox = QtWidgets.QCheckBox(Dialog)
        self.checkBox.setObjectName("checkBox")
        self.checkBox.stateChanged.connect(self.check_selected)

        self.verticalLayout.addWidget(self.checkBox)
        self.checkBox_2 = QtWidgets.QCheckBox(Dialog)
        self.checkBox_2.setObjectName("checkBox_2")
        self.checkBox_2.stateChanged.connect(self.check_selected)

        self.verticalLayout.addWidget(self.checkBox_2)
        self.checkBox_3 = QtWidgets.QCheckBox(Dialog)
        self.checkBox_3.setObjectName("checkBox_3")
        self.checkBox_3.stateChanged.connect(self.check_selected)

        self.verticalLayout.addWidget(self.checkBox_3)
        self.horizontalLayout.addLayout(self.verticalLayout)
        self.verticalLayout_2.addLayout(self.horizontalLayout)
        self.label_3 = QtWidgets.QLabel(Dialog)
        self.label_3.setObjectName("label_3")
        self.verticalLayout_2.addWidget(self.label_3)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def check_selected(self):
        price = 10

        if self.checkBox.isChecked():
            price = price + 3

        if self.checkBox_2.isChecked():
            price = price + 4

        if self.checkBox_3.isChecked():
            price = price + 5

        self.label_3.setText(f"Total Price Is: {price}")

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "Regular Tuna Price: 10"))
        self.label_2.setText(_translate("Dialog", "Select More"))
        self.checkBox.setText(_translate("Dialog", "Pizza: 3"))
        self.checkBox_2.setText(_translate("Dialog", "Salad: 4"))
        self.checkBox_3.setText(_translate("Dialog", "Sausage: 2"))
        self.label_3.setText(_translate("Dialog", "TextLabel"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec())

posted @   deadright  阅读(95)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示