2016-09-12 3 views
3

私はQt-Guiを使用して、バックグラウンドで作業を行い、guiのコンテンツを更新するいくつかのタスクを実装しようとしています。ここで私が取り組んでいるコードは(最小限に単純化)です。 GUIなしで、すなわち端末に印刷し、このコードは正常に動作します:終わりPython:Qt-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の交互作用です。

大変ありがとうございます。

folowingは、あなたがしたい場合は、 "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> 
+0

私はあなたのGUIがapp.exec()の中でのみ実行されることを認識している限り、C++ Qtから来ています。したがって、GUIのメインループを開始する前にスレッドのループが開始して終了します。上記のコードで – Hayt

答えて

1

PyQt4asyncio存在について認識していないです。 しかし、quamashは、Qtとasyncioの間のブリッジとして使用してください。アンドリューSvetlov、作業コードが提案したようquamash使用

+0

は、今追加しました: を 'quamashインポートQEventLoop' から: 'ループ= QEventLoop(APP) asyncio.set_event_loop(ループ) ' 代わりに: 'ループ= asyncio.get_event_loop() ' このように動作します。ありがとうございました – fava

2

は次のようになりましたになります。

#!/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_()) 

ありがとうございました!

関連する問題