所以我有这个 GUI,每当选中该复选框时我都会尝试更改它的样式表。这是我的代码的简化版本:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
style_sheet = "#Form{background-color: rgb(255,255,255);}"
inverted_ss = "#Form{background-color: rgb(0,0,0);}"
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(698, 453)
Form.setStyleSheet(style_sheet)
#some design preferences here, including frame_19 and horizontalLayout_5 as seen below
self.checkBox = QtWidgets.QCheckBox(self.frame_19)
self.checkBox.setEnabled(True)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.checkBox.sizePolicy().hasHeightForWidth())
self.checkBox.setSizePolicy(sizePolicy)
self.checkBox.setMinimumSize(QtCore.QSize(29, 25))
self.checkBox.setMaximumSize(QtCore.QSize(29, 50))
self.checkBox.setBaseSize(QtCore.QSize(25, 25))
self.checkBox.setLayoutDirection(QtCore.Qt.LeftToRight)
self.checkBox.setAutoFillBackground(False)
self.checkBox.setText("")
self.checkBox.setIconSize(QtCore.QSize(21, 21))
self.checkBox.setCheckable(True)
self.checkBox.stateChanged.connect(self.mode_change)
self.checkBox.setTristate(False)
self.checkBox.setObjectName("checkBox")
self.horizontalLayout_5.addWidget(self.checkBox)
#some more design preferences here
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "App"))
def mode_change(self, state):
if state == QtCore.Qt.Checked:
Form.setStyleSheet(inverted)
else:
Form.setStyleSheet(style_sheet)
问题是,它给了我一个警告:“未解析的引用:'Form'”。关于如何让它发挥作用有什么想法吗?
阿波罗的战车
相关分类