Skip to main content
 首页 » 编程设计

python之在单独的线程中运行 pyQT GUI 主应用程序

2024年08月22日38www_RR

我正在尝试在我已经建立的应用程序中添加一个 PyQt GUI 控制台。但是 PyQt GUI 会阻塞整个应用程序,使其无法完成其余工作。我尝试使用 QThread,但它是从 mainWindow 类调用的。我想要的是在单独的线程中运行 MainWindow 应用程序。

def main() 
      app = QtGui.QApplication(sys.argv) 
      ex = Start_GUI() 
      app.exec_()  #<---------- code blocks over here ! 
 
      #After running the GUI, continue the rest of the application task 
      doThis = do_Thread("doThis") 
      doThis.start() 
      doThat = do_Thread("doThat") 
      doThat.start() 

我的应用程序已经使用 Python 线程,所以我的问题是,以线程形式实现此过程的最佳方法是什么。

请您参考如下方法:

一种方法是

import threading 
 
def main() 
      app = QtGui.QApplication(sys.argv) 
      ex = Start_GUI() 
      app.exec_()  #<---------- code blocks over here ! 
 
#After running the GUI, continue the rest of the application task 
 
t = threading.Thread(target=main) 
t.daemon = True 
t.start() 
 
doThis = do_Thread("doThis") 
doThis.start() 
doThat = do_Thread("doThat") 
doThat.start() 

这将首先线程化您的主应用程序,然后让您在下面的代码中继续执行您想要执行的所有其他操作。