0

我一直在寻求让 PyQt 和 SimPy 相互“交谈”。例如,在下面的代码中,我有一个带有单个标签的 PyQt 小部件,用于显示 SimPy 环境时间。我希望这个标签随着模拟的进行而更新(我试图用这个simpy_generator函数来展示它——小部件中的标签被更新,SimPy 超时一个单位时间,并且这种模式重复)。

import sys
import simpy
from PyQt5 import QtWidgets

class Window(QtWidgets.QWidget):
    """ A generic Qt window that can interact with SimPy.
    """

    def __init__(self, env, parent=None):
        """ Class constructor.

        Args:
            env: SimPy environment.
            parent: Optional parent of this widget.
        """
        super(Window, self).__init__()
        self.env = env
        self.init()

    def init(self) -> None:
        """ Initialise the layout of the widget. Just a label that displays the
        SimPy Environment time.
        """
        layout = QtWidgets.QVBoxLayout()
        self.label = QtWidgets.QLabel('SimPy Time: {}'.format(self.env.now))
        layout.addWidget(self.label)
        self.setLayout(layout)
        self.show()

    def update(self) -> None:
        """ Update method for the window; retrieve the current SimPy environment time
        and update the label.
        """
        self.label.setText("SimPy Time: {}".format(self.env.now))

def simpy_generator(env, window):
    """ Generator for SimPy simulation; just tick the environment's clock and update
    the QtWidget's fields.

    Args:
        env: SimPy environment.
        window: QtWidget to update with SimPy data.
    """
    while True:
        window.update()
        yield env.timeout(1)

if __name__ == "__main__":

    env = simpy.Environment()
    app = QtWidgets.QApplication(sys.argv)
    window = Window(env=env)

    ### These need to be incorporated into the Qt event queue somehow?
    # simpy_process = env.process(simpy_generator(env, window))
    # env.run(until=25)

    app.exec_()

但是,我对如何使它起作用感到非常困惑。我知道 Qt 需要管理它的事件队列,并且更新通常会通过一些QTimer发出信号来触发它链接到的某个插槽的运行(在这种情况下是Window'方法)来完成。update但这似乎与 SimPy 自己的事件队列不兼容(或者更确切地说,我太无知了,无法理解它们应该如何交互);生成器需要作为一个进程添加到环境中,然后环境设置为运行直到完成(参见代码的注释部分)。

谁能告诉我如何才能做到这一点?

4

1 回答 1

0

根据迈克尔的回答,SimPy 模拟是在独立线程上启动的。上面的代码被修改为包含以下内容:

def simpy_thread(env, window):
    """ Function to be run on a dedicated thread where the SimPy simulation will live.

    Args:
        env: SimPy environment.
        window: QtWidget to update with SimPy data.
    """
    simpy_process = env.process(simpy_generator(env, window))
    env.run(until=25)

if __name__ == "__main__":

    env = simpy.rt.RealtimeEnvironment(factor=1)

    # Create the Qt window
    app = QtWidgets.QApplication(sys.argv)
    window = Window(env=env)

    # Launch a dedicated SimPy thread that has a reference to the window for updating
    thread = threading.Thread(target=simpy_thread, args=(env,window,))
    thread.start()

    # Finally, start the Qt event queue; the code will block at this point until the window
    # object is destroyed
    app.exec_()

仍然很想知道人们是否有替代解决方案:)

于 2021-09-15T21:40:53.000 回答