【发布时间】:2018-01-15 07:44:12
【问题描述】:
我正在尝试从多个线程更新 pyqt QProgressBar,据我所知,最好的方法是将信号发送回主 GUI 线程(我尝试将 QProgressBar 对象传递给工作线程,尽管它似乎确实有效,我在解释器中收到了大量警告)。在下面的代码中,我设置了一个 progressSignal 信号并将其连接到一个线程,该线程(目前)只打印发出的任何内容。然后我从每个线程发出总百分比。我知道这在线程之外工作,只需在第 47 行抛出一个随机发射,它确实通过了。然而,第 36 行的发射不会触发任何事情,所以它似乎永远无法通过......
import Queue, threading
from PyQt4 import QtCore
import shutil
import profile
fileQueue = Queue.Queue()
class Communicate(QtCore.QObject):
progressSignal = QtCore.pyqtSignal(int)
class ThreadedCopy:
totalFiles = 0
copyCount = 0
lock = threading.Lock()
def __init__(self, inputList, progressBar="Undefined"):
self.totalFiles = len(inputList)
self.c = Communicate()
self.c.progressSignal.connect(self.updateProgressBar)
print str(self.totalFiles) + " files to copy."
self.threadWorkerCopy(inputList)
def CopyWorker(self):
while True:
self.c.progressSignal.emit(2000)
fileName = fileQueue.get()
shutil.copy(fileName[0], fileName[1])
fileQueue.task_done()
with self.lock:
self.copyCount += 1
percent = (self.copyCount * 100) / self.totalFiles
self.c.progressSignal.emit(percent)
def threadWorkerCopy(self, fileNameList):
for i in range(16):
t = threading.Thread(target=self.CopyWorker)
t.daemon = True
t.start()
for fileName in fileNameList:
fileQueue.put(fileName)
fileQueue.join()
self.c.progressSignal.emit(1000)
def updateProgressBar(self, percent):
print percent
更新:
这是一个带有 gui 的示例。这个运行但很不稳定,它经常崩溃,UI 做了一些奇怪的事情(进度条没有完成等)
Main.py:
import sys, os
import MultithreadedCopy_5
from PyQt4 import QtCore, QtGui
def grabFiles(path):
# gets all files (not folders) in a directory
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
yield os.path.join(path, file)
class MainWin(QtGui.QWidget):
def __init__(self):
super(MainWin, self).__init__()
self.initUI()
def initUI(self):
self.progress = QtGui.QProgressBar()
box = QtGui.QVBoxLayout()
box.addWidget(self.progress)
goBtn = QtGui.QPushButton("Start copy")
box.addWidget(goBtn)
self.setLayout(box)
goBtn.clicked.connect(self.startCopy)
def startCopy(self):
files = grabFiles("folder/with/files")
fileList = []
for file in files:
fileList.append([file,"folder/to/copy/to"])
MultithreadedCopy_5.ThreadedCopy(fileList, self.progress)
def main():
app = QtGui.QApplication(sys.argv)
ex = MainWin()
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
多线程Copy_5.py:
import Queue, threading
from PyQt4 import QtCore
import shutil
import profile
fileQueue = Queue.Queue()
class Communicate(QtCore.QObject):
progressSignal = QtCore.pyqtSignal(int)
class ThreadedCopy:
totalFiles = 0
copyCount = 0
lock = threading.Lock()
def __init__(self, inputList, progressBar="Undefined"):
self.progressBar = progressBar
self.totalFiles = len(inputList)
self.c = Communicate()
self.c.progressSignal.connect(self.updateProgressBar, QtCore.Qt.DirectConnection)
print str(self.totalFiles) + " files to copy."
self.threadWorkerCopy(inputList)
def CopyWorker(self):
while True:
fileName = fileQueue.get()
shutil.copy(fileName[0], fileName[1])
fileQueue.task_done()
with self.lock:
self.copyCount += 1
percent = (self.copyCount * 100) / self.totalFiles
self.c.progressSignal.emit(percent)
def threadWorkerCopy(self, fileNameList):
for i in range(16):
t = threading.Thread(target=self.CopyWorker)
t.daemon = True
t.start()
for fileName in fileNameList:
fileQueue.put(fileName)
fileQueue.join()
def updateProgressBar(self, percent):
self.progressBar.setValue(percent)
#profile.run('ThreadedCopy()')
【问题讨论】:
-
我开始意识到 python 线程不能将信号发送回主 pyqt 应用程序,因此“正确”的方法是使用 pyqt QThreads - 我不想这样做。每次工作线程完成时,是否有一种简单的方法可以在主线程中发出信号?
-
请参阅我的答案以获得至少应该解决问题中显示的示例的问题。
标签: python multithreading pyqt pyqt4 qt-signals