PyQt5 bakground-image按钮中的样式表将其转换为无图像按钮

时间:2017-07-12 23:14:49

标签: python pyqt pyqt5 qtstylesheets qpushbutton

我使用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 #############

1 个答案:

答案 0 :(得分:1)

问题是由于没有正确使用名称引起的,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()完全相同。

有关详细信息,请阅读以下内容: