我试图了解如何在Pyqt中使用线程,并且在努力学习任何教程方面都感到很苦恼,因为大多数教程都是基于从头开始通过类创建的接口。我已经使用Qt设计器创建了我的界面,并且无法弄清楚如何使用线程来运行程序并更新该界面而不冻结它。我的示例代码如下所示:
import sys
from PyQt5 import uic, QtWidgets
def task():
for i in range (1,100000):
html = ("<p style = 'color:blue;'> <b> Completed: %s <b> <p><br>") % i
window.plainTextEdit.appendHtml(html)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = uic.loadUi('test.ui')
window.show()
window.pushButton.clicked.connect(task)
sys.exit(app.exec_())
也希望添加一个进度条,该进度条也可以在自己的线程上运行。
答案 0 :(得分:1)
您需要爱的课程,越早越好! 您想要的一种选择可能如下所示:
import sys
import threading
from PyQt5 import QtWidgets, QtCore # ,uic
def thread(my_func):
""" Runs a function in a separate thread. """
def wrapper(*args, **kwargs):
my_thread = threading.Thread(target=my_func, args=args, kwargs=kwargs)
my_thread.start()
return wrapper
@thread
def processing(signal):
""" Emulates the processing of some data. """
ind = 1
for i in range (1,10001): # +++
html = ("<p style='color:blue;'> Completed: <b>%s </b> </p> <br>") % i
QtCore.QThread.msleep(5)
ind = ind if i%100!=0 else ind+1 # +++
signal.emit(html, ind) # +++
def mySignalHandler(html, val): # Called to process a signal
plainTextEdit.appendHtml(html)
progressBar.setValue(val)
class WindowSignal(QtWidgets.QWidget):
""" New signals can be defined as class attributes using the pyqtSignal() factory. """
my_signal = QtCore.pyqtSignal(str, int, name='my_signal')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = WindowSignal()
button = QtWidgets.QPushButton("Emit your signal!", window)
button.clicked.connect(lambda: processing(window.my_signal))
# since you do not publish test.ui, I replaced it with the line below:
plainTextEdit = QtWidgets.QPlainTextEdit(window)
progressBar = QtWidgets.QProgressBar()
progressBar.setTextVisible(False)
layoutHBox = QtWidgets.QHBoxLayout()
layoutHBox.addWidget(button)
layoutHBox.addWidget(plainTextEdit)
layoutVBox = QtWidgets.QVBoxLayout()
window.setLayout(layoutVBox)
layoutVBox.addLayout(layoutHBox)
layoutVBox.addWidget(progressBar)
window.my_signal.connect(mySignalHandler, QtCore.Qt.QueuedConnection)
window.show()
sys.exit(app.exec_())
示例2:
test.ui
是一个文件,其中包含Qt Designer自动生成的主窗口的描述。
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>12</x>
<y>12</y>
<width>371</width>
<height>281</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="button">
<property name="text">
<string>Emit your signal!</string>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="plainTextEdit"/>
</item>
</layout>
</item>
<item>
<widget class="QProgressBar" name="progressBar">
<property name="value">
<number>0</number>
</property>
<property name="textVisible">
<bool>false</bool>
</property>
<property name="invertedAppearance">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
main.py
import sys
import threading
from PyQt5 import QtWidgets, QtCore, uic
def thread(my_func):
""" Runs a function in a separate thread. """
def wrapper(*args, **kwargs):
my_thread = threading.Thread(target=my_func, args=args, kwargs=kwargs)
my_thread.start()
return wrapper
@thread
def processing(signal):
""" Emulates the processing of some data. """
for i in range (1,101): # (1,100000)
html = ("<p style='color:blue;'> Completed: <b>%s </b> </p> <br>") % i
QtCore.QThread.msleep(10)
signal.emit(html, i) # Send a signal in which we transfer the received data
def mySignalHandler(html, val): # Called to process a signal
window.plainTextEdit.appendHtml(html)
window.progressBar.setValue(val)
class WindowSignal(QtWidgets.QWidget):
""" New signals can be defined as class attributes using the pyqtSignal() factory. """
my_signal = QtCore.pyqtSignal(str, int, name='my_signal')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = WindowSignal()
uic.loadUi('test.ui', window)
window.button.clicked.connect(lambda: processing(window.my_signal))
window.my_signal.connect(mySignalHandler, QtCore.Qt.QueuedConnection)
window.show()
sys.exit(app.exec_())