胡峻誠
胡峻誠

Reputation: 23

Pyqt5 Check pushbutton color and change pushbutton color

I created a pushbutton by PyQt5. The pushbutton's original background-color is gray. If its background-color is gray, its background-color will change to red when I press it. If its background-color is red, it will change to gray when I press it. The following is my code.

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(270, 200, 221, 71))
        self.pushButton.setStyleSheet("background-color: gray;")
        self.pushButton.setText("")
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.action)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

    def action(self):
        if self.pushButton.setStyleSheet["background-color"] == "gray":
            self.pushButton.setStyleSheet("background-color: red;")
        else:
            self.pushButton.setStyleSheet("background-color: gray;")
if __name__ == "__main__":
   import sys
   app = QtWidgets.QApplication(sys.argv)
   MainWindow = QtWidgets.QMainWindow()
   ui = Ui_MainWindow()
   ui.setupUi(MainWindow)
   MainWindow.show()
   sys.exit(app.exec_())

The code has a error in "if" line. I don't know how to check pushbutton background-color. If I use tkinter to create button. I know how to check button background-color.

if self.buttons["bg"] == "gray":

But now I want to know how to check pushbutton background-color by using PyQt5.

if self.pushButton.setStyleSheet["background-color"] == "gray":

Upvotes: 0

Views: 1564

Answers (1)

musicamante
musicamante

Reputation: 48250

Your code doesn't work because you're using square brackets (that are used for getting specific items from objects such as lists or dictionaries) against a function.
Also, setStyleSheet() is used to set the stylesheet while you might want to check against the existing stylesheet, which is returned with styleSheet().

This will fix the error and get the color switch working:

        if self.pushButton.styleSheet() == "background-color: gray;":

I would not suggest this approach anyway, as this will only work as long as the stylesheet string is exactly that.

Unfortunately, there's no way to programmatically get properties set through stylesheets.
When using setStyleSheet, its contents are internally parsed and processed by Qt, and the properties that are set are never exposed; you don't get an "object" you can interact with, so there's no way to do something like if "background-color" == "gray".

This also means that if you use a more complex css, with your approach you'll need to check against the whole contents of the style sheet and set it again completely, since setStyleSheet completely overwrites the existing one.

If you want to stick with css, you could create a "template" stylesheet (also allowing you to add other properties) and use an internal property to keep track of the color:

class Ui_MainWindow(object):
    buttonStyleSheet = """
        color: white;
        background-color: {};
    """
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(270, 200, 221, 71))
        self.pushButton.currentBackground = "gray"
        self.pushButton.setStyleSheet(self.buttonStyleSheet.format(self.pushButton.currentBackground))
        self.pushButton.setText("button")
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.action)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

    def action(self):
        if self.pushButton.currentBackground == "gray":
            self.pushButton.currentBackground = "red"
            self.pushButton.setStyleSheet(self.buttonStyleSheet.format("red"))
        else:
            self.pushButton.currentBackground = "gray"
            self.pushButton.setStyleSheet(self.buttonStyleSheet.format("gray"))

PS: I strongly suggest you to not edit the python files generated by pyuic to create your programs; read the official documentation on how to correctly use the files created with Designer instead.

Upvotes: 1

Related Questions