[英]Python PySide and Progress Bar Threading
我有這個代碼:
from PySide import QtCore, QtGui
import time
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 133)
self.progressBar = QtGui.QProgressBar(Dialog)
self.progressBar.setGeometry(QtCore.QRect(20, 10, 361, 23))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(20, 40, 361, 61))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Dialog", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.progressBar.setValue(0)
self.pushButton.clicked.connect(self.progress)
def progress(self):
self.progressBar.minimum = 1
self.progressBar.maximum = 100
for i in range(1, 101):
self.progressBar.setValue(i)
time.sleep(0.1)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
我希望在一個單獨的線程中有進度條,所以它不會凍結應用程序,但我似乎無法找到如何這樣做。
有人可以幫忙嗎?
我想你可能弄錯了。 您希望在單獨的線程中執行的工作因此不會凍結應用程序。 但您也希望能夠更新進度條。 您可以通過使用QThread
創建工作類來實現此目的。 QThreads能夠發出信號,您的UI可以監聽並采取適當的行動。
首先,讓我們創建你的工人類。
#Inherit from QThread
class Worker(QtCore.QThread):
#This is the signal that will be emitted during the processing.
#By including int as an argument, it lets the signal know to expect
#an integer argument when emitting.
updateProgress = QtCore.Signal(int)
#You can do any extra things in this init you need, but for this example
#nothing else needs to be done expect call the super's init
def __init__(self):
QtCore.QThread.__init__(self)
#A QThread is run by calling it's start() function, which calls this run()
#function in it's own "thread".
def run(self):
#Notice this is the same thing you were doing in your progress() function
for i in range(1, 101):
#Emit the signal so it can be received on the UI side.
self.updateProgress.emit(i)
time.sleep(0.1)
所以現在你有了一個工人階級,是時候利用它了。 您需要在Ui_Dialog
類中創建一個新函數來處理發出的信號。
def setProgress(self, progress):
self.progressBar.setValue(progress)
當你在那里時,你可以刪除progress()
函數。
在retranslateUi()
您將需要更新按鈕事件處理程序
self.pushButton.clicked.connect(self.progress)
至
self.pushButton.clicked.connect(self.worker.start)
最后,在你的setupUI()
函數中,你需要創建一個worker類的實例,並將它的信號連接到你的setProgress()
函數。
在這之前:
self.retranslateUi(Dialog)
添加這個:
self.worker = Worker()
self.worker.updateProgress.connect(self.setProgress)
這是最終的代碼:
from PySide import QtCore, QtGui
import time
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 133)
self.progressBar = QtGui.QProgressBar(Dialog)
self.progressBar.setGeometry(QtCore.QRect(20, 10, 361, 23))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(20, 40, 361, 61))
self.pushButton.setObjectName("pushButton")
self.worker = Worker()
self.worker.updateProgress.connect(self.setProgress)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.progressBar.minimum = 1
self.progressBar.maximum = 100
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Dialog", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.progressBar.setValue(0)
self.pushButton.clicked.connect(self.worker.start)
def setProgress(self, progress):
self.progressBar.setValue(progress)
#Inherit from QThread
class Worker(QtCore.QThread):
#This is the signal that will be emitted during the processing.
#By including int as an argument, it lets the signal know to expect
#an integer argument when emitting.
updateProgress = QtCore.Signal(int)
#You can do any extra things in this init you need, but for this example
#nothing else needs to be done expect call the super's init
def __init__(self):
QtCore.QThread.__init__(self)
#A QThread is run by calling it's start() function, which calls this run()
#function in it's own "thread".
def run(self):
#Notice this is the same thing you were doing in your progress() function
for i in range(1, 101):
#Emit the signal so it can be received on the UI side.
self.updateProgress.emit(i)
time.sleep(0.1)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
QThreads有一些自動發出的內置信號。 您可以在文檔中看到它們以及有關QThreads的更多信息
認為你總是需要使用多線程來做這樣的事情是錯誤的。
如果您可以將長時間運行的任務分解為一系列小步驟,那么您需要做的就是確保經常處理任何待處理事件,以便GUI保持響應。 這可以安全地從主界面線程內通過完成processEvents ,就像這樣:
for i in range(1, 101):
self.progressBar.setValue(i)
QtGui.qApp.processEvents()
time.sleep(0.1)
鑒於它的簡單性,在選擇更多重量級解決方案(如多線程或多處理)之前,至少考慮這種技術總是值得的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.