0

I have a class which inherits from QWidget.

Push button click is connected with some_function.

I would like to change PushButton color to red before function starts doing its core functionality and change it to green when the core will be finished, but i have no idea how to change PushButton color outside of some_button.

EDIT: I added button to the instance attributes as @dudakl suggested but color still changes after whole function finishing running, instead to red at the beginning and green at the end.

from PyQt5.QtWidgets import *

class AppWidget(QWidget):


    def __init__(self, parent = None):
        super(AppWidget, self).__init_(parent)

        mainLayout = QGridLayout()
        self.some_button()
        mainLayout.addWidget(self.someButton)

        self.setLayout(mainLayout)
        self.show()


    def some_button(self):

        self.someButton = QGroupBox('Some GroupBox')
        layout = QVBoxLayout()

        button = QPushButton('Button')
        button.clicked.connect(self.some_function)
        layout.addWidget(button)


    def some_function(self):

        #change color to red
        #do something
        #change color to green
w8eight
  • 605
  • 1
  • 6
  • 21
  • When do you delay in executing "do something"? if it takes more than 10ms then you should not run it in the GUI thread but in another one because otherwise it will freeze the GUI – eyllanesc Jul 01 '19 at 12:47
  • The following code is an example of how to perform this task in another thread https://gist.github.com/eyllanesc/27d88036c8e769fc4e2aad342b7fdca7 – eyllanesc Jul 01 '19 at 12:59

1 Answers1

0

You could add the button to the instance-attributes with:

self.button = QPushbutton('Button')

and then address it in "some_function" with

self.button.something

Check this post for the color change:

dudakl
  • 302
  • 1
  • 8
  • I tried, but the color of the button is changing after whole function is finished. Most important thing for me is, to change color to red while function is running, and to green after it is finished. Core of the function takes some time to process, colors are to show if user can click another functionality. You are right about accessing color of the button outside its original function but it still does not resolving my problem. Thanks anyway. – w8eight Jul 01 '19 at 12:40
  • You will need to trigger a refresh of your screen after setting the color of the button. So the flow would be: #change color to red #refresh screen #do something #change color to green #refresh screen – Dennis Jensen Jul 01 '19 at 16:12