简体   繁体   English

PyQt5 中的 ProgressBar 用于多处理

[英]ProgressBar in PyQt5 for multiprocessing

I have implemented a progress bar in my pythonGUI script and when the run button is clicked it executes another python script in which I have used multiprocessing for fetching query from database and genrating a output excel.我在我的 pythonGUI 脚本中实现了一个进度条,当单击运行按钮时,它会执行另一个 python 脚本,在该脚本中我使用多处理从数据库中获取查询并生成 output ZBF57C906FA7D2BB66D07372E4 Suppose there is about 10 query to execute and generate 10 excel outputs.假设有大约 10 个查询要执行并生成 10 个 excel 输出。 In this case, how do I implement progress bar for multiprocessing.?在这种情况下,如何实现多处理的进度条。?

Thanks you in advance提前谢谢你

Given that I had none of your code to go off of, I made a few assumptions about what you are using and the overall structure.鉴于我没有您的 go 代码,我对您使用的内容和整体结构做了一些假设。 Hopefully however, there is enough info here so that you can adapt this to your needs and your specific code.但是,希望这里有足够的信息,以便您可以根据自己的需要和特定代码进行调整。

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!')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM