简体   繁体   中英

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. Suppose there is about 10 query to execute and generate 10 excel outputs. 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. 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!')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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