Python:Qt-Gui和一些任务
作者:互联网
我正在尝试使用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>
解决方法:
PyQt4不知道异步存在.
但是quamash可以,请使用它作为Qt和asyncio之间的桥梁.
标签:multitasking,pyqt,python-asyncio,python,qt 来源: https://codeday.me/bug/20191118/2026233.html