简体   繁体   中英

Quamash QventLoop “RuntimeError: no running event loop” error in python and PyQt5

I don't seem to find the right solution to this error. The program keep giving "RuntimeError: no running event loop". Why is the event loop not running?

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()

This is one of the example online which am using to learn this concept. It seems to work for other students programmers but for me it gives me the error highlighted above.

Quamash has not been active since July 2018, so it has numerous bugs that have not been solved, due to this inactivity, fork such as qasync ( python -m pip install qasync ) and asyncqt ( python -m pip install asyncqt ) have been created, so it recommends you to use one of those libraries, and for this it only changes to:

from qasync import QEventLoop, QThreadExecutor

OR

from asyncqt import QEventLoop, QThreadExecutor

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