我正在尝试在新进程中打开第二个窗口,以便不使用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()函数时,会显示消息“正在打开窗口..”,但不会发生任何其他情况。没有窗口,没有错误......我无法弄清楚发生了什么。提前谢谢!
答案 0 :(得分:0)
我已经找到了解决方案。您必须创建一个新的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_())