0
如何在PyQt5中使用Python3异步调用方法?如何在PyQt5中使用Python3异步调用方法?
我试图用信号来做到这一点。
import sys
from time import sleep
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
class MainWindow(QMainWindow):
asyncFuncSignal = pyqtSignal()
def __init__(self):
super().__init__()
self.initUi()
def initUi(self):
self.label = QLabel(self)
self.label.setText("loading...")
# Trying to call 'self.asyncFunc' asynchronously
self.asyncFuncSignal.connect(self.asyncFunc)
self.asyncFuncSignal.emit()
print("loaded")
def asyncFunc(self):
# Doing something hard that takes time
# I have used 'sleep' to implement the delay
sleep(2)
self.label.setText("done")
print("asyncFunc finished")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
此程序试图写"loaded"
前完成asyncFunc
。但我希望程序立即完成initUi
,并在标签中显示loading...
,之后文本done
在2秒内出现。
什么是最好的和最短的做法呢?
这里说的是http://pyqt.sourceforge.net/Docs/PyQt5/signals_slots.html我可以使用排队连接,但我还没有找到如何实现它的例子。