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_())