连续闪烁 Qbushbuttom 在 Thread 中无法正常工作

问题描述 投票:0回答:2

我想在我的程序运行时闪烁一个 Qbushbuttom,我尝试用线程来实现它,它工作了几次然后它不再闪烁了

`    def blinker_station(self,station):
     while(1):
       #print('station,self.stationblinkervalue[station] : ',station,self.stationblinkervalue[station]) 
       if self.stationblinkervalue[station]==2 :
         self.buttom_stations[station].setStyleSheet("background-color: red")
         
         time.sleep(0.5)
        
         time.sleep(0.5)
       elif self.stationblinkervalue[station]==1:
         self.buttom_stations[station].setStyleSheet("background-color: red")
     
     time.sleep(1)
     
     self.buttom_stations[station].setStyleSheet("background-color: white")
     
     time.sleep(0.5)
    
     time.sleep(0.5)
   elif self.stationblinkervalue[station]==0:
     self.buttom_stations[station].setStyleSheet("background-color: white")
     
     time.sleep(0.5)
     
     time.sleep(0.5)`

也就是说,我在 MainWindows init 末尾写的内容:

for i in range(len(self.stationMatrix)):
          random.seed(5)
         
          self.station_threading_blinker.append(threading.Thread(target=self.blinker_station, args=(i,)))
          self.station_threading_blinker[-1].start()
          time.sleep(random.random()+1)

有人可以帮我解决这个问题吗,我怎样才能让我的 qbushbottom 不断闪烁

python python-3.x pyqt pyqt5 python-multithreading
2个回答
1
投票

我不确定切换样式表以更改背景颜色是否是使按钮闪烁的最有效方法,但是 QTimer 是进行计时的方法。

你在定时器上设置一个短时间间隔(比如 500 毫秒)并将它的超时信号连接到一个方法来切换按钮上的背景颜色。

import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QHBoxLayout
from PyQt5.QtCore import QTimer


class Window(QWidget):
    def __init__(self):
        super().__init__()
        layout = QHBoxLayout(self)
        self.blinker = QPushButton("I'm blinking")
        layout.addWidget(self.blinker)

        self.background_colour = 'red'

        self.blink_timer = QTimer()
        self.blink_timer.setInterval(500)
        self.blink_timer.timeout.connect(self.switch_background_colour)
        self.blink_timer.start()

    def switch_background_colour(self):
        if self.background_colour == 'red':
            self.background_colour = 'white'
        elif self.background_colour == 'white':
            self.background_colour = 'red'

        self.blinker.setStyleSheet(f"background-color: {self.background_colour}")


if __name__ == "__main__":
    app = QApplication([])
    window = Window()
    window.show()
    sys.exit(app.exec())

0
投票

我现在有解决方案了!

我在 MainWindows Init 中写的内容:

        for self.i in range(len(self.stationMatrix)):
                  self.worker_thread.append(WorkerThread())
                  self.worker_thread[self.i].gui_text = self.i
                  self.worker_thread[self.i].blinker_mode = 0
                  self.worker_thread[self.i].job_done.connect(self.on_job_done)
                  self.worker_thread[self.i].start()
                  time.sleep(0.5)
     #End of INIT             
#########################################################################################################
    def on_job_done(self, t):
        #print("Generated string : ", t)
        # self.buttom_stations[t[1]].setStyleSheet(t[0])
        matrix=t.split('/')
        self.buttom_stations[int(matrix[1])].setStyleSheet(matrix[0])

那是另一个类 WorkerThread 可以完成工作:

class WorkerThread(QtCore.QThread):
    job_done = QtCore.pyqtSignal('QString')
    def __init__(self, parent=None):
        super(WorkerThread, self).__init__(parent)
        self.gui_text = None
        self.blinker_mode = None
        self.x="background-color: white"
    def do_work(self):
        
        t=' '
        while(1):
         #   print(self.gui_text)
            if self.blinker_mode==1:
             if self.x =="background-color: red" : 
                 t="background-color: white"+'/'+str(self.gui_text)
                 self.job_done.emit(t)
                 self.x="background-color: white"
                 time.sleep(0.5)
             if self.x =="background-color: white" :
              t="background-color: red"+'/'+str(self.gui_text)
              self.job_done.emit(t)
              self.x="background-color: red"
              time.sleep(0.5)
            if self.blinker_mode==0:
            #   if self.x =="background-color: white" : 
                 t="background-color: white"+'/'+str(self.gui_text)
                 self.job_done.emit(t)
                 self.x="background-color: white"
                 time.sleep(1)
            if self.blinker_mode==2:
            #   if self.x =="background-color: red" : 
                 t="background-color: red"+'/'+str(self.gui_text)
                 self.job_done.emit(t)
                 self.x="background-color: red"
                 time.sleep(1)
             
            
    def run(self):
        self.do_work()

现在如果我想改变闪烁我只需要打电话:

self.worker_thread[index_of_worker].blinker_mode = Mode_of_blinker

© www.soinside.com 2019 - 2024. All rights reserved.