我想在池进程并行执行时更新 GIF。现在 GIF 卡住了,它正在响应进度条值更新。我希望 GIF 独立执行。我尝试过使用它的线程,但这不是我的流程的解决方案。一旦控制要进行池处理,它就会冻结 GUI,没有响应。我已经给出了这段 GIF 中不应该卡住的高级代码。我正在通过原始代码中的进程复制多个文件,在那里完成任务需要很长时间,直到 GUI 没有响应。所以池过程我无法删除或替换。让我知道无论进度条和进程执行如何,这种 GIF 连续移动是否可行。我会很感激代码更改,因为我是 python 和 PYQT5 的新手。
提前致谢。
from PyQt5.QtGui import QMovie
import sys
import time
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import (QApplication, QDialog,
QProgressBar, QPushButton, QVBoxLayout, QLabel)
from multiprocessing import Pool, freeze_support
TIME_LIMIT = 100
def func(a, b):
print(a +b)
class External (QThread):
countChanged = pyqtSignal(int)
def run(self):
count = 0
print("check1")
while count < TIME_LIMIT:
count += 1
time.sleep(1)
print("thread1 update")
self.countChanged.emit(count)
class Actions (QDialog):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Progress Bar')
self.setGeometry(200,300,200,200)
col = QVBoxLayout(self)
self.text = ['ON', 'OFF']
self.label = QLabel(self)
self.movie = QMovie("movie.gif")
self.label.setMovie(self.movie)
self.progress = QProgressBar(self)
self.progress.setGeometry(0, 0, 300, 25)
self.progress.setMaximum(100)
self.button = QPushButton('Start', self)
col.addWidget(self.label)
col.addWidget(self.progress)
col.addWidget(self.button)
self.label.move(0,90)
self.button.move(0, 30)
self.button.clicked.connect(self.onButtonClick)
self.show()
def startAnimation(self):
self.movie.start()
def onButtonClick(self):
self.startAnimation()
self.calc = External()
a_args = [1, 2, 3, 4, 5, 6]
second_arg = 1
self.calc.countChanged.connect(self.onCountChanged)
self.calc.start()
QApplication.processEvents()
for i in a_args:
pool = Pool(processes=3)
M = pool.starmap(func, zip(a_args, repeat(second_arg)))
QApplication.processEvents()
pool.close()
pool.join()
print("value of args",i)
def onCountChanged(self, value):
self.progress.setValue(value)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Actions()
sys.exit(app.exec_())