首页 > 解决方案 > 从python中的另一个线程更改为主线程

问题描述

例如,有没有办法从后台线程转移到主线程;

import threading

def func(txt):
    if txt == 'foo':
        ## must do the processing in the main thread
        pass

    else:
        print(txt)


thread = threading.Thread(target = func, args = ('hello',))
thread.start()

提前致谢 :)

标签: pythonpython-3.xmultithreadingpyqt5

解决方案


您可以在主线程中有一个变量,该变量将在 func 中更改。然后检查 main 变量的状态。

如果 foo,那么做点什么。


推荐阅读