PyQt5 背景图像按钮中的样式表将其变成无图像按钮
Style-sheets in PyQt5 bakground-image pushbutton turns it into a no-image pushbutton
我使用 Qt-Designer 创建了一个主窗口,并使用样式表使按钮在按下时更改图像。但是在 运行 之后背景图像和按下的图像不起作用。
这是从 Qt-Designer 输入的样式表:
QPushButton#House1 {
border-image: url(:/img/Crystal_Clear_app_kfm_home.png);
}
QPushButton#House:pressed {
border-image: url(:/img/Search_in_home.png);
}
这是我的 mainWindow.py :
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import resources_rc
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(811, 600)
MainWindow.setStyleSheet("background-color: rgb(211, 211, 211);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
###################house 1#######
self.house2 = QtWidgets.QPushButton(self.centralwidget)
self.house1.setGeometry(QtCore.QRect(61, 21, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house1.sizePolicy().hasHeightForWidth())
self.house1.setSizePolicy(sizePolicy)
self.house1.setStyleSheet("#House1 {\n"
"border-image: url(:/img/Crystal_Clear_app_kfm_home.png);\n"
"}\n"
"#House:Pressed {\n"
" border-image: url(:/img/Search_in_home.png);\n"
"}")
self.house1.setText("")
self.house1.setObjectName("house1")
####################end #############
self.house4 = QtWidgets.QPushButton(self.centralwidget)
self.house4.setGeometry(QtCore.QRect(61, 203, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house4.sizePolicy().hasHeightForWidth())
self.house4.setSizePolicy(sizePolicy)
self.house4.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house4.setText("")
self.house4.setObjectName("house4")
self.house2 = QtWidgets.QPushButton(self.centralwidget)
self.house2.setGeometry(QtCore.QRect(293, 21, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house2.sizePolicy().hasHeightForWidth())
self.house2.setSizePolicy(sizePolicy)
self.house2.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house2.setText("")
self.house2.setObjectName("house2")
self.house3 = QtWidgets.QPushButton(self.centralwidget)
self.house3.setGeometry(QtCore.QRect(524, 21, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house3.sizePolicy().hasHeightForWidth())
self.house3.setSizePolicy(sizePolicy)
self.house3.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house3.setText("")
self.house3.setObjectName("house3")
self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_8.setGeometry(QtCore.QRect(293, 384, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_8.sizePolicy().hasHeightForWidth())
self.pushButton_8.setSizePolicy(sizePolicy)
self.pushButton_8.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.pushButton_8.setText("")
self.pushButton_8.setObjectName("pushButton_8")
self.house5 = QtWidgets.QPushButton(self.centralwidget)
self.house5.setGeometry(QtCore.QRect(293, 203, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house5.sizePolicy().hasHeightForWidth())
self.house5.setSizePolicy(sizePolicy)
self.house5.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house5.setText("")
self.house5.setObjectName("house5")
self.house6 = QtWidgets.QPushButton(self.centralwidget)
self.house6.setGeometry(QtCore.QRect(524, 203, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house6.sizePolicy().hasHeightForWidth())
self.house6.setSizePolicy(sizePolicy)
self.house6.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house6.setText("")
self.house6.setObjectName("house6")
self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_7.setGeometry(QtCore.QRect(61, 384, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_7.sizePolicy().hasHeightForWidth())
self.pushButton_7.setSizePolicy(sizePolicy)
self.pushButton_7.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.pushButton_7.setText("")
self.pushButton_7.setObjectName("pushButton_7")
self.pushButton_9 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_9.setGeometry(QtCore.QRect(524, 384, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_9.sizePolicy().hasHeightForWidth())
self.pushButton_9.setSizePolicy(sizePolicy)
self.pushButton_9.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.pushButton_9.setText("")
self.pushButton_9.setObjectName("pushButton_9")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 811, 27))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
self.menuAdmin = QtWidgets.QMenu(self.menubar)
self.menuAdmin.setObjectName("menuAdmin")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionSave = QtWidgets.QAction(MainWindow)
self.actionSave.setObjectName("actionSave")
self.actionSave_As = QtWidgets.QAction(MainWindow)
self.actionSave_As.setObjectName("actionSave_As")
self.actionLogin = QtWidgets.QAction(MainWindow)
self.actionLogin.setObjectName("actionLogin")
self.actionSign_Up = QtWidgets.QAction(MainWindow)
self.actionSign_Up.setObjectName("actionSign_Up")
self.menuFile.addAction(self.actionOpen)
self.menuFile.addAction(self.actionSave)
self.menuFile.addAction(self.actionSave_As)
self.menuAdmin.addAction(self.actionLogin)
self.menuAdmin.addAction(self.actionSign_Up)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuAdmin.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "SWTDI Main Window"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.menuAdmin.setTitle(_translate("MainWindow", "Admin"))
self.actionOpen.setText(_translate("MainWindow", "Open"))
self.actionSave.setText(_translate("MainWindow", "Save"))
self.actionSave_As.setText(_translate("MainWindow", "Save As"))
self.actionLogin.setText(_translate("MainWindow", "Login"))
self.actionSign_Up.setText(_translate("MainWindow", "Sign Up"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(w)
w.show()
sys.exit(app.exec())
资源文件:
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>img/house.png</file>
<file>img/house2.png</file>
<file>img/NM_State_University_logo.png</file>
<file>img/NMlogo_1colorstate_black.png</file>
<file>img/NMlogo_1colorstate_red.tif</file>
<file>img/Crystal_Clear_app_kfm_home.png</file>
<file>img/Gohomenew.png</file>
<file>img/Search_in_home.png</file>
</qresource>
</RCC>
按钮是由我注释掉的行创建的。但无论如何
我用我见过的类似代码创建了另一个按钮。
###################house 1#######
self.house1 = QPushButton(self.centralwidget)
#self.house1 = QtWidgets.QPushButton(self.centralwidget)
self.house1.setGeometry(QtCore.QRect(61, 21, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house1.sizePolicy().hasHeightForWidth())
self.house1.setSizePolicy(sizePolicy)
self.house1.setStyleSheet("#House1 {\n"
"border-image: url(:/img/Crystal_Clear_app_kfm_home.png);\n"
"}\n"
"#House:Pressed {\n"
" border-image: url(:/img/Search_in_home.png);\n"
"}")
self.house1.setText("")
self.house1.setObjectName("house1")
####################end #############
问题是由于没有正确使用名称引起的,QStyleSheet 使用 objectName 属性,在你的例子中是 QPushButton self.house1:
self.house1.setObjectName("house1")
在编写该行之前,按钮没有 objectName(实际上它的 objectName 是“”),因此不适合按 objectName 放置 QStyleSheet。
您应该做的是放置一个 objectName,然后您可以按名称分配过滤后的 QStyleSheet。
self.house1 = QtWidgets.QPushButton(self.centralwidget)
self.house1.setObjectName("house1")
self.house1.setStyleSheet("#house1 {\n"
"border-image: url(:/img/Crystal_Clear_app_kfm_home.png);\n"
"}\n"
"#house1:Pressed {\n"
" border-image: url(:/img/Search_in_home.png);\n"
"}")
此属性区分大小写,因此您必须将其放置在与 setObjectName()
.
完全相同的位置
有关更多信息,您应该阅读以下内容:
我使用 Qt-Designer 创建了一个主窗口,并使用样式表使按钮在按下时更改图像。但是在 运行 之后背景图像和按下的图像不起作用。
这是从 Qt-Designer 输入的样式表:
QPushButton#House1 {
border-image: url(:/img/Crystal_Clear_app_kfm_home.png);
}
QPushButton#House:pressed {
border-image: url(:/img/Search_in_home.png);
}
这是我的 mainWindow.py :
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import resources_rc
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(811, 600)
MainWindow.setStyleSheet("background-color: rgb(211, 211, 211);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
###################house 1#######
self.house2 = QtWidgets.QPushButton(self.centralwidget)
self.house1.setGeometry(QtCore.QRect(61, 21, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house1.sizePolicy().hasHeightForWidth())
self.house1.setSizePolicy(sizePolicy)
self.house1.setStyleSheet("#House1 {\n"
"border-image: url(:/img/Crystal_Clear_app_kfm_home.png);\n"
"}\n"
"#House:Pressed {\n"
" border-image: url(:/img/Search_in_home.png);\n"
"}")
self.house1.setText("")
self.house1.setObjectName("house1")
####################end #############
self.house4 = QtWidgets.QPushButton(self.centralwidget)
self.house4.setGeometry(QtCore.QRect(61, 203, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house4.sizePolicy().hasHeightForWidth())
self.house4.setSizePolicy(sizePolicy)
self.house4.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house4.setText("")
self.house4.setObjectName("house4")
self.house2 = QtWidgets.QPushButton(self.centralwidget)
self.house2.setGeometry(QtCore.QRect(293, 21, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house2.sizePolicy().hasHeightForWidth())
self.house2.setSizePolicy(sizePolicy)
self.house2.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house2.setText("")
self.house2.setObjectName("house2")
self.house3 = QtWidgets.QPushButton(self.centralwidget)
self.house3.setGeometry(QtCore.QRect(524, 21, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house3.sizePolicy().hasHeightForWidth())
self.house3.setSizePolicy(sizePolicy)
self.house3.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house3.setText("")
self.house3.setObjectName("house3")
self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_8.setGeometry(QtCore.QRect(293, 384, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_8.sizePolicy().hasHeightForWidth())
self.pushButton_8.setSizePolicy(sizePolicy)
self.pushButton_8.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.pushButton_8.setText("")
self.pushButton_8.setObjectName("pushButton_8")
self.house5 = QtWidgets.QPushButton(self.centralwidget)
self.house5.setGeometry(QtCore.QRect(293, 203, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house5.sizePolicy().hasHeightForWidth())
self.house5.setSizePolicy(sizePolicy)
self.house5.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house5.setText("")
self.house5.setObjectName("house5")
self.house6 = QtWidgets.QPushButton(self.centralwidget)
self.house6.setGeometry(QtCore.QRect(524, 203, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house6.sizePolicy().hasHeightForWidth())
self.house6.setSizePolicy(sizePolicy)
self.house6.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house6.setText("")
self.house6.setObjectName("house6")
self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_7.setGeometry(QtCore.QRect(61, 384, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_7.sizePolicy().hasHeightForWidth())
self.pushButton_7.setSizePolicy(sizePolicy)
self.pushButton_7.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.pushButton_7.setText("")
self.pushButton_7.setObjectName("pushButton_7")
self.pushButton_9 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_9.setGeometry(QtCore.QRect(524, 384, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_9.sizePolicy().hasHeightForWidth())
self.pushButton_9.setSizePolicy(sizePolicy)
self.pushButton_9.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.pushButton_9.setText("")
self.pushButton_9.setObjectName("pushButton_9")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 811, 27))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
self.menuAdmin = QtWidgets.QMenu(self.menubar)
self.menuAdmin.setObjectName("menuAdmin")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionSave = QtWidgets.QAction(MainWindow)
self.actionSave.setObjectName("actionSave")
self.actionSave_As = QtWidgets.QAction(MainWindow)
self.actionSave_As.setObjectName("actionSave_As")
self.actionLogin = QtWidgets.QAction(MainWindow)
self.actionLogin.setObjectName("actionLogin")
self.actionSign_Up = QtWidgets.QAction(MainWindow)
self.actionSign_Up.setObjectName("actionSign_Up")
self.menuFile.addAction(self.actionOpen)
self.menuFile.addAction(self.actionSave)
self.menuFile.addAction(self.actionSave_As)
self.menuAdmin.addAction(self.actionLogin)
self.menuAdmin.addAction(self.actionSign_Up)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuAdmin.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "SWTDI Main Window"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.menuAdmin.setTitle(_translate("MainWindow", "Admin"))
self.actionOpen.setText(_translate("MainWindow", "Open"))
self.actionSave.setText(_translate("MainWindow", "Save"))
self.actionSave_As.setText(_translate("MainWindow", "Save As"))
self.actionLogin.setText(_translate("MainWindow", "Login"))
self.actionSign_Up.setText(_translate("MainWindow", "Sign Up"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(w)
w.show()
sys.exit(app.exec())
资源文件:
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>img/house.png</file>
<file>img/house2.png</file>
<file>img/NM_State_University_logo.png</file>
<file>img/NMlogo_1colorstate_black.png</file>
<file>img/NMlogo_1colorstate_red.tif</file>
<file>img/Crystal_Clear_app_kfm_home.png</file>
<file>img/Gohomenew.png</file>
<file>img/Search_in_home.png</file>
</qresource>
</RCC>
按钮是由我注释掉的行创建的。但无论如何 我用我见过的类似代码创建了另一个按钮。
###################house 1#######
self.house1 = QPushButton(self.centralwidget)
#self.house1 = QtWidgets.QPushButton(self.centralwidget)
self.house1.setGeometry(QtCore.QRect(61, 21, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house1.sizePolicy().hasHeightForWidth())
self.house1.setSizePolicy(sizePolicy)
self.house1.setStyleSheet("#House1 {\n"
"border-image: url(:/img/Crystal_Clear_app_kfm_home.png);\n"
"}\n"
"#House:Pressed {\n"
" border-image: url(:/img/Search_in_home.png);\n"
"}")
self.house1.setText("")
self.house1.setObjectName("house1")
####################end #############
问题是由于没有正确使用名称引起的,QStyleSheet 使用 objectName 属性,在你的例子中是 QPushButton self.house1:
self.house1.setObjectName("house1")
在编写该行之前,按钮没有 objectName(实际上它的 objectName 是“”),因此不适合按 objectName 放置 QStyleSheet。
您应该做的是放置一个 objectName,然后您可以按名称分配过滤后的 QStyleSheet。
self.house1 = QtWidgets.QPushButton(self.centralwidget)
self.house1.setObjectName("house1")
self.house1.setStyleSheet("#house1 {\n"
"border-image: url(:/img/Crystal_Clear_app_kfm_home.png);\n"
"}\n"
"#house1:Pressed {\n"
" border-image: url(:/img/Search_in_home.png);\n"
"}")
此属性区分大小写,因此您必须将其放置在与 setObjectName()
.
有关更多信息,您应该阅读以下内容: