PyQt 多个QThread的确认完成

2024-09-29 23:33:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Qt-Creator和Python创建了一个GUI工具。在GUI中,用户将选择他/她想要生成的shapefile的类型。脚本将选择存储在列表中。每种类型的shapefile的生成都由单独的qthread处理。每个QThread都会打印一些消息,比如“Processing AAA….”。“运行”按钮功能将执行以下操作:

if optionA in selection_list:
    QThreadA.start()
if optionB in selection_list:
    QThreadB.start()
if optionC in selection_list:
    QThreadC.start()

现在我正在尝试让GUI在所有QThreads完成后弹出一个窗口。我在这里发现的https://riverbankcomputing.com/pipermail/pyqt/2007-May/016026.htmlQThread.wait等待()的工作原理与线程连接(),我正在努力利用它。你知道吗

我做不到

if optionA in selection_list:
    QThreadA.start()
if optionB in selection_list:
    QThreadB.start()
if optionC in selection_list:
    QThreadC.start()

QThreadA.wait()
QThreadB.wait()
QThreadC.wait()
print("All set")

因为有时并非所有的qthread都在运行。你知道吗

但如果我这么做了

if optionA in selection_list:
    QThreadA.start()
if optionB in selection_list:
    QThreadB.start()
if optionC in selection_list:
    QThreadC.start()

if optionA in selection_list:
    QThreadA.wait()
if optionB in selection_list:
    QThreadB.wait()
if optionC in selection_list:
    QThreadC.wait()
print("All set")

这不仅看起来很蠢,当我按“运行”按钮时,我还会

"All set"
"Processing AAA..."
"Processing BBB..."
"Processing CCC..." 

显然,当一切都结束时,这些信息就出现了,而不是我所希望看到的:

"Processing AAA..."
some time passed...
"Processing BBB..."
some more time passed...
"Processing CCC..."
some more time passed...
"All set"

这里发生的事情的顺序是什么?这基本上使得消息毫无用处,因为用户只有在所有事情最终完成时才会看到它们。你知道吗

在如何重建函数方面需要一些帮助。你知道吗

编辑:根据@Nimish Bansal的推荐,我想到了这个:

dict = {optionA: QThreadA, optionB: QThreadB, optionC: QThreadC}
for option in selection_list:
    dict[option].start()

为了避免之前愚蠢的“如果”,但我仍然没有找到一个有效的方法来总结所有的线程,因为:

for options in selection_list:
    dict[option].wait()

似乎不起作用,因为我猜.wait()会暂停for循环吗?你知道吗


Tags: inifallstartlistwaitsetprocessing
1条回答
网友
1楼 · 发布于 2024-09-29 23:33:58
def run_fun(a):
    print("processing",a)
    time.sleep(2)

import threading
A=threading.Thread(target=run_fun,args=('aaa',))
B=threading.Thread(target=run_fun,args=('bbb',))
C=threading.Thread(target=run_fun,args=('ccc',))




def join_threads(listi):
    for j in listi:
        j.join()
    print("ALL set","open your gui here")



A.start()
B.start()
C.start()
threading.Thread(target=join_threads,args=([A,B,C],)).start()

输出

正在处理aaa

处理bbb

处理ccc

所有设置在这里打开你的gui

相关问题 更多 >

    热门问题