源码
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'qt_checkbox.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(324, 356)
self.checkBox = QtWidgets.QCheckBox(MainWindow)
self.checkBox.setGeometry(QtCore.QRect(20, 20, 91, 19))
self.checkBox.setObjectName("checkBox")
self.pushButton = QtWidgets.QPushButton(MainWindow)
self.pushButton.setGeometry(QtCore.QRect(20, 90, 201, 28))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.getCheckboxStatus)
self.pushButton_2 = QtWidgets.QPushButton(MainWindow)
self.pushButton_2.setGeometry(QtCore.QRect(20, 140, 201, 28))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_2.clicked.connect(self.setCheckboxChecked)
self.pushButton_3 = QtWidgets.QPushButton(MainWindow)
self.pushButton_3.setGeometry(QtCore.QRect(20, 190, 201, 28))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_3.clicked.connect(self.setCheckboxUnchecked)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.checkBox.setText(_translate("MainWindow", "CheckBox"))
self.pushButton.setText(_translate("MainWindow", "Get Checkbox Status"))
self.pushButton_2.setText(_translate("MainWindow", "Set Checkbox Checked"))
self.pushButton_3.setText(_translate("MainWindow", "Set Checkbox Unchecked"))
def show(self, Form):
Form.show()
def messageBox(self,msg):
msgBox = QtWidgets.QMessageBox()
msgBox.setIcon(QtWidgets.QMessageBox.Icon.Information)
msgBox.setWindowTitle("Qt Message Box")
msgBox.setText(msg)
msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel)
returnValue = msgBox.exec()
def getCheckboxStatus(self) :
if self.checkBox.isChecked():
self.messageBox("Checkbox is Marked")
else:
self.messageBox("Checkbox is Unmarked")
def setCheckboxChecked(self):
self.checkBox.setChecked(True)
def setCheckboxUnchecked(self):
self.checkBox.setChecked(False)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Form=QtWidgets.QWidget()
main_win = Ui_MainWindow()
main_win.setupUi(Form)
main_win.show(Form)
sys.exit(app.exec())
截图: