[英]ProgressBar in PyQt5 for multiprocessing
我在我的 pythonGUI 腳本中實現了一個進度條,當單擊運行按鈕時,它會執行另一個 python 腳本,在該腳本中我使用多處理從數據庫中獲取查詢並生成 output ZBF57C906FA7D2BB66D07372E4 假設有大約 10 個查詢要執行並生成 10 個 excel 輸出。 在這種情況下,如何實現多處理的進度條。?
提前謝謝你
鑒於我沒有您的 go 代碼,我對您使用的內容和整體結構做了一些假設。 但是,希望這里有足夠的信息,以便您可以根據自己的需要和特定代碼進行調整。
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.