我似乎没有找到解决此错误的正确方法。该程序不断给出“RuntimeError:没有正在运行的事件循环”。为什么事件循环没有运行?
import sys
import asyncio
import time
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar, QMessageBox
from quamash import QEventLoop, QThreadExecutor
class QuamashTrial(QWidget):
def __init__(self):
super(QuamashTrial, self).__init__()
self.initialize_widgets()
loop.run_until_complete(self.master())
QMessageBox.information(self, " ", 'It is done.')
def initialize_widgets(self):
vbox = QVBoxLayout()
self.progress = QProgressBar()
self.progress.setRange(0, 99)
self.progress.show()
self.setLayout(vbox)
@asyncio.coroutine
def master(self):
yield from self.first_50()
with QThreadExecutor(1) as exec:
yield from loop.run_in_executor(exec, self.last_50)
@asyncio.coroutine
def first_50(self):
for i in range(50):
self.progress.setValue(i)
yield from asyncio.sleep(.05)
def last_50(self):
for i in range(50,100):
loop.call_soon_threadsafe(self.progress.setValue, i)
time.sleep(.05)
if __name__ == "__main__":
app = QApplication(sys.argv)
loop = QEventLoop(app)
asyncio.set_event_loop(loop)
with loop:
q = QuamashTrial()
q.show()
loop.run_forever()
这是用于学习此概念的在线示例之一。它似乎适用于其他学生程序员,但对我来说它给了我上面突出显示的错误。
最佳答案
Quamash 自 2018 年 7 月以来一直没有事件,因此它有许多尚未解决的错误,由于这种不活动, fork 如 qasync
(python -m pip install qasync
) 和 asyncqt
(python -m pip install asyncqt
) 已创建,因此建议您使用这些库之一,为此它仅更改为:
from qasync import QEventLoop, QThreadExecutor
或
from asyncqt import QEventLoop, QThreadExecutor
关于python - python 和 PyQt5 中的 Quamash QventLoop "RuntimeError: no running event loop"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61858880/