简体   繁体   中英

How to wait for a function to finish before executing the subsequent function PyQt5

In my following "Simon Says" game, I have created a PushButton Subclass with a function that will make the button blink for a duration of x seconds.

class BlinkingPushButton(QPushButton):
    def __init__(self, text, color):
        super().__init__()
        self.setStyleSheet(f"""
            border-radius: 30px;
            background-color: {color};
            font: Futura;
            font-size: 54px;
            color: white;
            font-weight: bold;
        """)
        self.ss = self.styleSheet()
        self.setText(text)
        self.setFixedSize(250, 250)

    def blink(self, duration):
        self.setStyleSheet(self.ss + "background-color: white;")
        t = threading.Timer(duration, self.setStyleSheet, [self.ss])
        t.start()

In my MainWindow, I have a function flash which should flash the buttons in the order of the values in self.sequence

    def flash(self):
        # self.sequence = ['R', 'B', 'G', 'Y']
        # self.red, self.blue, self.green, and self.yellow are all of the BlinkingPushButton class
        for value in self.sequence:
            if value == 'R':
                self.red.blink(duration=1)
            elif value == 'B':
                self.blue.blink(duration=1)
            elif value == 'G':
                self.green.blink(duration=1)
            elif value == 'Y':
                self.yellow.blink(duration=1)

The issue is that each button, despite the functions being called in a specific order, all blink asynchronously.

How can I get the buttons to blink synchronously, in the order listed in self.sequence ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM