Pyside / Pyqt Opening new windows from a window (Event loop is already 运行)

Pyside / Pyqt Opening new windows from a window (Event loop is already running)

我正在尝试创建一个 UI,它将充当启动所有其他已创建工具的中心。问题是,如果我尝试从 toolshub UI 启动 UI,它不会让我启动,因为事件循环已经 运行。我知道在启动新的 window 时我不能执行此 APP = QtGui.QApplication(sys.argv) 和 APP.exec_() 因为事件循环已经是 运行 时我为 toolshub UI 做了那个。但我想不出其他方法。

这是其中一种工具的示例代码,可自行启动。

global APP
APP = None

class toolwindow(QtGui.QDialog):

    def __init__(self, parent=None):
        init_app()
        super(toolwindow, self).__init__(parent)
        self.setWindowTitle('tool')
        self.setMinimumSize(QtCore.QSize(500, 600))
        self.setMaximumSize(QtCore.QSize(500, 600))
        self.create_ui()

    def create_ui(self):
        code goes here

    def closeEvent(self, event):
        QtGui.QDialog.closeEvent(self, event)
        return

def init_app():
    global APP
    APP = QtGui.QApplication.instance()
    if not APP:
        APP = QtGui.QApplication(sys.argv)
    return

def start_ui():
    win= toolwindow()
    win.show()
    APP.exec_()

if __name__ == '__main__':
    start_ui()
global APP
APP = None

现在这是工具集的代码 ui。这些都是单独的脚本。在 toolshub 中,我正在导入上面的工具。

import tool
LOGGER = logging.getLogger(__name__)
global APP
APP = None

class toolsHub(QtGui.QDialog):

    def __init__(self, parent=None):
        init_app()
        super(toolsHub, self).__init__(parent)
        self.setWindowTitle('Tools Launcher')
        self.setSizeGripEnabled(False)
        self.setMinimumSize(QtCore.QSize(300, 200))
        self.setMaximumSize(QtCore.QSize(300, 200))
        self.create_layouts()

    def create_layouts(self):
        master_layout = QtGui.QVBoxLayout()
        self.setLayout(master_layout)
        self.input_widgets()
        grid_layout = QtGui.QGridLayout()
        grid_layout.addWidget(self.env_creator, 0, 0)
        grid_layout.addWidget(self.p4dl, 1, 0)
        master_layout.addLayout(grid_layout)

    def input_widgets(self):
        self.tool_button= QtGui.QPushButton('launch tool')
        self.tool_button.clicked.connect(self.launch_tool)

    def launch_tool(self):
        tool.start_ui()

    def closeEvent(self, event):
        QtGui.QDialog.closeEvent(self, event)
        return


def init_app():
    global APP
    APP = QtGui.QApplication.instance()
    if not APP:
        APP = QtGui.QApplication(sys.argv)
    return

def start_ui():
    toolui = toolsHub()
    toolui.show()
    APP.exec_()

if __name__ == '__main__':
    start_ui()

那么如何编写和构建这些,以便我可以从工具中心 UI 打开工具 UI?我猜我必须更改的代码是工具 UI,我知道我必须取出 APP = QtGui.QApplication(sys.argv) 和 APP.exec_(),但不是确定如何启动它。

谢谢

QApplication 应该只有一个实例,并且应该在创建任何小部件之前创建它,因此没有必要在每个模块中实例化它。

为了正确显示 QDialog,您必须执行其 exec_()

tool.py:

[...]
def start_ui():
    win= toolwindow()
    win.exec_()

完整代码:

tool.py

from PySide import QtGui, QtCore

APP = None

class toolwindow(QtGui.QDialog):

    def __init__(self, parent=None):
        init_app()
        super(toolwindow, self).__init__(parent)
        self.setWindowTitle('tool')
        self.setMinimumSize(QtCore.QSize(500, 600))
        self.setMaximumSize(QtCore.QSize(500, 600))
        self.create_ui()

    def create_ui(self):
        pass
    def closeEvent(self, event):
        QtGui.QDialog.closeEvent(self, event)
        return

def init_app():
    global APP
    APP = QtGui.QApplication.instance()
    if not APP:
        APP = QtGui.QApplication(sys.argv)
    return

def start_ui():
    win= toolwindow()
    win.exec_()

if __name__ == '__main__':
    start_ui()

main.py

from PySide import QtGui, QtCore
import tool
import logging
import sys

LOGGER = logging.getLogger(__name__)
APP = None

class toolsHub(QtGui.QDialog):

    def __init__(self, parent=None):
        init_app()
        super(toolsHub, self).__init__(parent)
        self.setWindowTitle('Tools Launcher')
        self.setSizeGripEnabled(False)
        self.setMinimumSize(QtCore.QSize(300, 200))
        self.setMaximumSize(QtCore.QSize(300, 200))
        self.create_layouts()

    def create_layouts(self):
        master_layout = QtGui.QVBoxLayout()
        self.setLayout(master_layout)
        self.input_widgets()
        grid_layout = QtGui.QGridLayout()
        grid_layout.addWidget(QtGui.QPushButton(), 0, 0)
        grid_layout.addWidget(QtGui.QPushButton(), 1, 0)
        master_layout.addLayout(grid_layout)

    def input_widgets(self):
        self.tool_button= QtGui.QPushButton('launch tool')
        self.tool_button.clicked.connect(self.launch_tool)
        self.layout().addWidget(self.tool_button)

    def launch_tool(self):
        tool.start_ui()

    def closeEvent(self, event):
        QtGui.QDialog.closeEvent(self, event)


def init_app():
    global APP
    APP = QtGui.QApplication.instance()
    if not APP:
        APP = QtGui.QApplication(sys.argv)
    return

def start_ui():
    toolui = toolsHub()
    toolui.show()
    APP.exec_()

if __name__ == '__main__':
    start_ui()