0

So i have a button called RunButton. When I press it I want it to change a button's background color from red to green when it hits the 3 second mark, in a 10 seconds range. This is how I do it:

self.RunButton.clicked.connect(self.run)

def run(self):
    for i in range(10):
        i += 1
        print i
        time.sleep(1)
        if (i == 3):
            self.B18.setStyleSheet("background-color: green")

Although, when i click the RunButton it prints the time as it goes by but the button B18 only changes color when the for loop reaches it's end, i.e., after 10 seconds, when it should have changed after 3.

So how do I make to change color mid loop, when it should be?

1 Answer 1

3

first of all, while you are blocking the main thread, there cannot be a ui update.

using QApplication::processEvents() (its from c++ but it should be the same in python) should helop you with that.

the other thing is, you could create a QTimer that triggers the recoloring like this (pseudocode, no guarantee to be valid):

#class initialisation
self.timer = QTimer()
self.timer.setInterval(3)
self.timer.setSingleShot(True)

self.timer.timeout.connect(lambda x: self.B18.setStyleSheet("background-color: green"))
self.RunButton.clicked.connect(self.timer.start)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.