Python:Qt-Gui和几个任务

时间:2016-09-12 14:32:15

标签: python qt pyqt multitasking python-asyncio

我正在尝试使用Qt-Gui并在那里实现几项任务,以便在后台进行工作并更新gui中的内容。这是我正在处理的代码(简化到最小)。没有gui,即打印到终端,这段代码工作正常:

 #!/usr/bin/env python3

import sys
import asyncio
from PyQt4 import QtCore, QtGui, uic

qtCreatorFile = "gui_mini_task.ui"  # Enter file here.

Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)


class MyApp(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)

@asyncio.coroutine
def task_1(futue_1):
    for i in range(50):
        window.results_window.setText("task_1")
        yield from asyncio.sleep(.1)

@asyncio.coroutine
def task_2(future_2):
    for i in range(1, 10):
        window.results_window.setText("task_2")
        yield from asyncio.sleep(0.5)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MyApp()

    future_1 = asyncio.Future()
    future_2 = asyncio.Future()
    tasks = [
        asyncio.async(task_1(future_1)),
        asyncio.async(task_2(future_2))]
    loop = asyncio.get_event_loop()
    window.show() 

    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()
    sys.exit(app.exec_())

最后,即5秒后,主窗口打开,似乎同时执行了这两项任务。但我的目的是实时(或多或少)实时查看消息,即task_1和task_2的交替。

感谢您的宝贵帮助!

以下是“gui_mini_task.ui”的代码,如果您想要...

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="enabled">
   <bool>true</bool>
  </property>
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QTextEdit" name="results_window">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>410</y>
      <width>731</width>
      <height>71</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>20</y>
      <width>161</width>
      <height>41</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>20</pointsize>
      <weight>75</weight>
      <bold>true</bold>
     </font>
    </property>
    <property name="text">
     <string>nellcor_gui</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>27</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <tabstops>
  <tabstop>results_window</tabstop>
 </tabstops>
 <resources/>
 <connections/>
</ui>

2 个答案:

答案 0 :(得分:2)

使用Andrew Svetlov提出的quamash,工作代码现在看起来像这样:

#!/usr/bin/env python3

import sys
import asyncio
from PyQt4 import QtCore, QtGui, uic
from quamash import QEventLoop

qtCreatorFile = "gui_mini_task.ui"  # Enter file here.

Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)


class MyApp(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)

@asyncio.coroutine
def task_1(futue_1):
    for i in range(50):
        window.results_window.setText("task_1")
        yield from asyncio.sleep(.1)

@asyncio.coroutine
def task_2(future_2):
    for i in range(1, 12):
        window.results_window.setText("task_2")
        yield from asyncio.sleep(0.5)


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

    future_1 = asyncio.Future()
    future_2 = asyncio.Future()
    tasks = [
        asyncio.async(task_1(future_1)),
        asyncio.async(task_2(future_2))]

    window.show()  # erst jetzt werden die Widgets instanziert!

    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()
    sys.exit(app.exec_())

谢谢!

答案 1 :(得分:1)

PyQt4不了解asyncio的存在。 但quamash确实如此,请将其用作Qt和asyncio之间的桥梁。