如何在python pyqt5中连续改变标签颜色颜色

How to change the label color color continuously in python pyqt5

我是 python 的新手,我正在尝试学习 simpy 和 pyqt5。我正在制作一些程序,我想在每次迭代时更改标签颜色,但我不知道为什么它只显示一种颜色。

要更改颜色,我首先提到绿色然后是黑色,但在启动程序后它只显示黑色。有什么办法可以连续改变颜色吗

def carParking(env, name, ps, depart_time, parking_Duration):
    yield env.timeout(depart_time)
    print("Car %d arrived on station at %d" % (name, env.now))
    app.processEvents()
    dlg.parkedCars.display(name)
    app.processEvents()
    dlg.lEnter.setStyleSheet('color: Green')
    dlg.lEnter.setStyleSheet('color: Black')
    dlg.lEnter.setStyleSheet('color: Green')
    time.sleep(0.30)
    with ps.request() as req:
        yield req
        time.sleep(0.30)
        print("%d parked at %s" % (name, env.now))
        yield env.timeout(parking_Duration)
        time.sleep(0.30)
        print("%d leaving the Parking Station at %s" % (name, env.now))
        dlg.lExit.setStyleSheet('color: Red')

在上面的代码中,您可以看到绿色、黑色和绿色,但它不是那样工作的。它只显示一种颜色,即绿色。 下面给出剩余代码

env = simpy.Environment()
ps = simpy.Resource(env, capacity=5)

def syslot(self):
    #a=dlg.parkingSpace.setText(str(float(dlg.nCars.text())))
    ab=int(dlg.nCars.text())
    for i in range(ab):
        a = randint(1, 5)
       # dlg.lEnter.setStyleSheet('color: black')
       # dlg.lExit.setStyleSheet('color: black')
        env.process(carParking(env,  i, ps, i * 2, a))
        time.sleep(0.10)
        print("The parking duration of Car %d is %d" % (i, a))
        env.run()

app =QtWidgets.QApplication([])
dlg = uic.loadUi("design.ui")
dlg.visualize.clicked.connect(syslot)
dlg.show()
sys.exit(app.exec_())

请告诉我如何连续更改标签颜色。提前谢谢你。

为了让用户感觉到变化,不能这么快,因为视觉是慢的,想法是给一个可察觉的时间,比如500ms。另一方面,循环必须在另一个线程中执行,以免阻塞 GUI 并通过信号与 GUI 通信。

综合以上,解决方案是:

import random
import threading
import time

import simpy

from PyQt5 import QtCore, QtWidgets, uic


def car_parking(env, name, ps, depart_time, parking_duration, manager):
    yield env.timeout(depart_time)
    print("Car %d arrived on station at %d" % (name, env.now))
    manager.valueChanged.emit(name)
    manager.entered.emit()
    time.sleep(0.3)
    with ps.request() as rq:
        yield rq
        time.sleep(0.3)
        print("%d parked at %s" % (name, env.now))
        yield env.timeout(parking_duration)
        time.sleep(0.30)
    print("%d leaving the Parking Station at %s" % (name, env.now))
    manager.exited.emit()


def loop(number_of_cars, manager):
    env = simpy.Environment()
    ps = simpy.Resource(env, capacity=5)

    for i in range(number_of_cars):
        t = random.randint(1, 5)
        env.process(car_parking(env, i, ps, i * 2, t, manager))
        time.sleep(0.3)
        print("The parking duration of Car %d is %d" % (i, t))
        env.run()


class Manager(QtCore.QObject):
    entered = QtCore.pyqtSignal()
    exited = QtCore.pyqtSignal()
    valueChanged = QtCore.pyqtSignal(int)

    def start(self, number_of_cars):
        threading.Thread(target=loop, args=(number_of_cars, self), daemon=True).start()


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        uic.loadUi("design.ui", self)
        self.manager = Manager()
        self.manager.valueChanged.connect(self.parkedCars.display)
        self.manager.entered.connect(self.on_entered)
        self.manager.exited.connect(self.on_exited)
        self.visualize.clicked.connect(self.on_clicked)

    @QtCore.pyqtSlot()
    def on_clicked(self):
        try:
            n = int(self.nCars.text())
            self.manager.start(n)
        except ValueError:
            print("It must be an integer")

    @QtCore.pyqtSlot()
    def on_entered(self):
        self.lEnter.setStyleSheet("color: green")
        QtCore.QTimer.singleShot(500, lambda: self.lEnter.setStyleSheet("color: black"))

    @QtCore.pyqtSlot()
    def on_exited(self):
        self.lExit.setStyleSheet("color: red")
        QtCore.QTimer.singleShot(500, lambda: self.lExit.setStyleSheet("color: black"))


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())