Quamash和PyQt5 - 在进程结束时出现的额外窗口

时间:2017-04-05 02:50:29

标签: python asynchronous pyqt5

美好的一天,我目前正在使用Quamash和PyQt学习异步过程。我试图将https://github.com/harvimt/quamash中的示例转换为类。我似乎做错了,因为在流程结束时会出现一个额外的窗口。我希望有人能引导我走向正确的方向。

谢谢!

还使用Python 3.4,PyQt5。

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

1 个答案:

答案 0 :(得分:1)

首先,您在show()函数和__main__函数中调用initialize_widgets()函数两次。所以这就是你看到额外窗口的原因。 其次,如果您使用loop.run_until_complete(self.master()),则无需致电loop.run_forever(),因为循环在self.master()完成后停止。

我认为您的主要功能应该是这样的:

if __name__ == "__main__":
    app = QApplication(sys.argv)
    loop = QEventLoop(app)
    asyncio.set_event_loop(loop)

    with loop:
        q = QuamashTrial()