CheckBox是复选框控件,可以进行多种选择。
任务目标:
设计一个窗口,实现选择课程的功能,完成选择后,可以显示选择的内容。
完整代码:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(366, 290)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.checkBox = QtWidgets.QCheckBox(self.centralwidget)
self.checkBox.setGeometry(QtCore.QRect(90, 70, 121, 19))
self.checkBox.setObjectName("checkBox")
self.checkBox_2 = QtWidgets.QCheckBox(self.centralwidget)
self.checkBox_2.setGeometry(QtCore.QRect(90, 100, 91, 19))
self.checkBox_2.setObjectName("checkBox_2")
self.checkBox_3 = QtWidgets.QCheckBox(self.centralwidget)
self.checkBox_3.setGeometry(QtCore.QRect(90, 40, 91, 19))
self.checkBox_3.setObjectName("checkBox_3")
self.checkBox_4 = QtWidgets.QCheckBox(self.centralwidget)
self.checkBox_4.setGeometry(QtCore.QRect(90, 130, 91, 19))
self.checkBox_4.setObjectName("checkBox_4")
self.checkBox_5 = QtWidgets.QCheckBox(self.centralwidget)
self.checkBox_5.setGeometry(QtCore.QRect(90, 160, 91, 19))
self.checkBox_5.setObjectName("checkBox_5")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(160, 190, 93, 28))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 366, 29))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.getvalue)#手动添加信号与槽函数相连
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def getvalue(self): #自定义getvalue()方法
oper="" # 记录用户权限
if self.checkBox.isChecked(): # 判断复选框是否选中
oper+=self.checkBox.text() # 记录选中的权限
if self.checkBox_2.isChecked():
oper +='\n'+ self.checkBox_2.text()
if self.checkBox_3.isChecked():
oper+='\n'+ self.checkBox_3.text()
if self.checkBox_4.isChecked():
oper+='\n'+ self.checkBox_4.text()
if self.checkBox_5.isChecked():
oper+='\n'+ self.checkBox_5.text()
from PyQt5.QtWidgets import QMessageBox
# 使用information()方法弹出信息提示,显示所有选择的权限
QMessageBox.information(MainWindow, "提示", "您选择的课程如下:\n"+oper, QMessageBox.Ok)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.checkBox.setText(_translate("MainWindow", "计算机网络"))
self.checkBox_2.setText(_translate("MainWindow", "需求工程"))
self.checkBox_3.setText(_translate("MainWindow", "高等数学"))
self.checkBox_4.setText(_translate("MainWindow", "操作系统"))
self.checkBox_5.setText(_translate("MainWindow", "离散数学"))
self.pushButton.setText(_translate("MainWindow", "确定"))
import sys
if __name__ == '__main__':
app=QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
效果: