PyQt5中的ProgressBar用于多处理

我已经在pythonGUI脚本中实现了一个进度条,当单击运行按钮时,它会执行另一个python脚本,在该脚本中,我使用了多重处理从数据库中获取查询并生成输出excel。假设有大约10个查询要执行并生成10个excel输出。在这种情况下,如何实现进度条以进行多处理??

提前谢谢

zwx10527 回答:PyQt5中的ProgressBar用于多处理

考虑到我没有要编写的代码,我对使用的东西和总体结构做了一些假设。但是,希望这里有足够的信息,以便您可以根据需要和特定的代码进行调整。

import PyQt5
from PyQt5 import QtWidgets
import threading

class MyClass(object):
    progressbar_progress = PyQt5.QtCore.pyqtSignal(int)

    def __init__(self):
        # progressBar refers to YOUR progress bar object in your interface
        # Change that name to whatever you've named your actual progress bar
        self.progressbar_progress.connect(self.progressbar.setValue)

    @staticmethod
    def start_thread(functionName,*args,**kwargs):
        if len(args) == 0:
            t = threading.Thread(target=functionName)
        else:
            try:
                t = threading.Thread(target=functionName,args=args,kwargs=kwargs)
            except:
                try:
                    if args is None:
                        t = threading.Thread(target=functionName,kwargs=kwargs)
                except:
                    t = threading.Thread(target=functionName)

        t.daemon = True
        t.start()

    def button_run_clicked(self):
        MyClass.start_thread(self.run_query)

# You would probably want to come up with a way to get a numerical value for your
# processes in your query. For example,if you know that you have to search through
# 10,000 entries,then that becomes your reference point. So the first query would
# be 1 out of 10,000,which would be .01% progress and the PyQt5 progress bar's
# display value is based on a percentage. So just send the current state's progress
# percentage to the signal that was created.

    def run_query(self):
        # All your querying code
        # For example,let's assume my_query is a list

        for i,q in enumerate(my_query):
            # Send the value to display to the signal like this
            self.progressBar.progress.emit(i / len(my_query))
        print('Done!')
本文链接:https://www.f2er.com/3091495.html

大家都在问