L
L
Limons2021-04-10 23:13:46
PyQt
Limons, 2021-04-10 23:13:46

QtDesinger + Pyqt + pushbutton how to change color on click?

There is a button whose Id = button, in QtDesinger it already has its own css.
I want the color to change to black (0, 0, 0) when pressed, but implement it in code.

def function():
        form.fishing_0.setStyleSheet('background: rgb(255,0,0);')

form.fishing_0.clicked.connect(function)

But this way it forgets the whole style of the button and all that remains of the button is text and bg, losing shadows, stroke, etc. What to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WolfInChains, 2021-04-12
@Limons

Either pass the entire style with a changed color (not just the background), or pass colors as arguments when calling the window

# стилизуем через f строки, передавая цвета как аргументы
self.pushButton.setStyleSheet("QPushButton{\n"
                                          "    border-radius: 30px;\n"
                                          f"    border: 2px solid {color_1};\n"
                                          f"    background-color:  {color_2};\n"
                                          "}")

# добавляем аргументы в setupUi
def setupUi(self, MainWindow, color_1, color_2):

# если форма как отдельный модуль
import your_form
self.form = your_form.class_name()

# если в одном файле
self.form = class_name()

# вызываем форму с новыми цветами
def function(self):
    self.form.setupUi(self, "rgb(255,0,0)", "rgb(255,0,0)")
    self.show()

self.form.fishing_0.clicked.connect(function)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question