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

Anu*_*raz 4 python user-interface multithreading pyqt pyqt4

我正在尝试在我已经建立的应用程序中添加 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()
Run Code Online (Sandbox Code Playgroud)

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

Pax*_*cum 5

这样做的一种方法是

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()
Run Code Online (Sandbox Code Playgroud)

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

  • 不支持在非主线程中使用任何 Qt GUI 代码,并且可能会导致各种有趣的崩溃。请参阅[Qt 文档](http://doc.qt.io/qt-5/thread-basics.html#gui-thread-and-worker-thread)。 (14认同)