我正在用QT5和python编写一个UI,我添加了一个线程来处理UI,thread的工作方式是“fine”,线程内部的一个函数接收2个字符串并返回2个字符串(我在开发真正的项目之前正在做实验,只是为了看看它是如何工作的),正如您在调用thread函数后的代码中看到的:
self.requestConexion.emit('lblText1','dddddd')
我调用另一个函数,它只是一个简单的计数器
^{pr2}$所以我希望在计数器完成控件的值之前自我.lblText1改变,但这不会发生。。。主要代码如下:
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import UI_test
import time
import sys
class Threaded(QObject):
result=pyqtSignal(str,str)
def __init__(self, parent=None, **kwargs):
super().__init__(parent, **kwargs)
@pyqtSlot(str,str)
def etiquetas(self,lbl,texto):
print(texto)
self.result.emit(lbl,texto)
class MainApp(QMainWindow, UI_test.Ui_MainWindow):
requestConexion=pyqtSignal(str,str)
def __init__(self, parent=None):
super(MainApp, self).__init__(parent)
self._thread=QThread()
self._threaded=Threaded(result=self.displayLabel)
self.requestConexion.connect(self._threaded.etiquetas)
self._threaded.moveToThread(self._thread)
qApp.aboutToQuit.connect(self._thread.quit)
self._thread.start()
self.setupUi(self)
self.btnStart.clicked.connect(self.conexion)
@pyqtSlot()
def conexion(self):
#self._thread.start()
print(1)
self.requestConexion.emit('lblText1','dddddd')
self.contador()
text, ok = QInputDialog.getText(self, 'Text Input Dialog', 'Enter your name:')
if ok:
print(str(text))
@pyqtSlot()
def contador(self):
i=0
while i<50:
print(i)
time.sleep(0.1)
i+=1
@pyqtSlot(str,str)
def displayLabel(self, etiqueta, texto):
self.lblText1.setText(etiqueta)
print(texto)
def main():
app = QApplication(sys.argv)
form = MainApp()
form.show()
app.exec_()
exit(app.exec_())
if __name__ == '__main__':
main()
你知道怎么回事吗?在
我终于在下一篇博客中找到了我问题的答案:
https://martinfitzpatrick.name/article/multithreading-pyqt-applications-with-qthreadpool/
这是一个非常棒的教程,在阅读了博客中的文档之后,我能够修改其中一个示例,以实时修改label控件的文本;以下是最终代码:
相关问题 更多 >
编程相关推荐