datetime:2019/5/20 15:45
author:nzb

复选框

import sys
from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, QLabel, QGroupBox, QCheckBox, QHBoxLayout
from PyQt5 import QtGui, QtCore


class UI_demo(QDialog):
    """用户界面"""
    def __init__(self):
        super().__init__()

        # 窗口信息
        self.title = 'PyQt5 Check Box'
        self.left = 600
        self.top = 200
        self.width = 500
        self.height = 200

        self.initWindow()

    def initWindow(self):

        # 窗口信息
        self.setWindowIcon(QtGui.QIcon('../img/home.ico'))  # 图标设置
        self.setGeometry(self.left, self.top, self.width, self.height)  # 大小位置设置
        self.setWindowTitle(self.title)  # 窗口标题

        self.CreateCheckBox()

        # 复选按钮
        vbox = QVBoxLayout()
        vbox.addWidget(self.groupBox)

        self.label = QLabel(self)
        self.label.setFont(QtGui.QFont('Sanserif', 15))  #设置标签字体字号
        vbox.addWidget(self.label)

        self.setLayout(vbox)

        # 展示窗口
        self.show()

    def CreateCheckBox(self):
        """复选框"""
        self.groupBox = QGroupBox("What is you favorite programming language?")
        self.groupBox.setFont(QtGui.QFont("Sanserif", 13))  # 设置字体字号
        hboxLayout = QHBoxLayout()

        self.check1 = QCheckBox("python")
        self.check1.setIcon(QtGui.QIcon('../img/python.ico'))
        self.check1.setIconSize(QtCore.QSize(40, 40))
        self.check1.setFont(QtGui.QFont('Sanserif', 13))  # 设置字体字号
        self.check1.toggled.connect(self.onCheckBox_Toggled)  # 绑定事件
        hboxLayout.addWidget(self.check1)

        self.check2 = QCheckBox("java")
        self.check2.setIcon(QtGui.QIcon('../img/java.ico'))
        self.check2.setIconSize(QtCore.QSize(40, 40))
        self.check2.setFont(QtGui.QFont('Sanserif', 13))  # 设置字体字号
        self.check2.toggled.connect(self.onCheckBox_Toggled)  # 绑定事件
        hboxLayout.addWidget(self.check2)

        self.check3 = QCheckBox("php")
        self.check3.setIcon(QtGui.QIcon('../img/php.ico'))
        self.check3.setIconSize(QtCore.QSize(40, 40))
        self.check3.setFont(QtGui.QFont('Sanserif', 13))  # 设置字体字号
        self.check3.toggled.connect(self.onCheckBox_Toggled)  # 绑定事件
        hboxLayout.addWidget(self.check3)

        self.groupBox.setLayout(hboxLayout)

    def onCheckBox_Toggled(self):
        """复选框触发事件"""
        if self.check1.isChecked():
            self.label.setText('you have select:' + self.check1.text())
        if self.check2.isChecked():
            self.label.setText('you have select:' + self.check2.text())
        if self.check3.isChecked():
            self.label.setText('you have select:' + self.check3.text())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = UI_demo()
    sys.exit(app.exec_())

results matching ""

    No results matching ""