我正在尝试在新进程中打开第二个窗口,以免使用 PyQt5 冻结主窗口。出于这个原因,我定义了一个继承自multiprocessing.Process
并显示窗口的新类。这是主要代码:
class GuiMain(QMainWindow):
...
# Main window with several functions. When a button is clicked, executes
# self.button_pressed()
def button_pressed(self):
proc1 = OpenWindowProcess()
proc1.start()
class OpenWindowProcess(mp.Process)
def __init__(self):
mp.Process.__init__(self)
print(self.pid)
def run(self):
print("Opening window...")
window = QtGui.QWindow()
window.show()
time.sleep(10)
if __name__ == '__main__':
app = QApplication(sys.argv)
application = GuiMain()
sys.exit(app.exec_())
该过程已创建并获取 PID。调用 run(( 函数时,将显示消息"打开窗口...",但不会发生任何其他操作。没有窗口,没有错误...我不知道发生了什么。提前谢谢你!
我已经找到了解决方案。您必须创建一个新的QtApplication并向其附加一个新的QMainWindow实例。此代码工作正常:
class GuiMain(QMainWindow):
...
# Main window with several functions. When a button is clicked, executes
# self.button_pressed()
def button_pressed(self):
proc1 = OpenWindowProcess()
proc1.start()
class OpenWindowProcess(mp.Process)
def __init__(self):
mp.Process.__init__(self)
print("Process PID: " + self.pid)
def run(self):
print("Opening window...")
app = QApplication(sys.argv)
window = QMainWindow()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
app = QApplication(sys.argv)
application = GuiMain()
sys.exit(app.exec_())