2017-07-06 77 views
0

我正在嘗試創建一個用作集線器的用戶界面,以啓動已創建的所有其他工具。問題是,如果我嘗試從toolshub UI啓動UI,它不會讓我因爲事件循環已經在運行。我知道當啓動新窗口時,我不能執行此應用程序= QtGui.QApplication(sys.argv)和APP.exec_(),因爲當我爲toolshub UI執行此操作時,事件循環已經在運行。但我無法弄清楚如何以另一種方式做到這一點。Pyside/Pyqt從窗口打開新窗口(事件循環已在運行)

以下是其中一個工具的示例代碼,它自行啓動。

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 

現在這裏是toolshub用戶界面的代碼。這些都是獨立的腳本。在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() 

那麼如何編寫和構造這些以便我可以從toolshub UI中打開工具UI?我猜測我必須改變的代碼是工具UI,我知道我必須取出APP = QtGui.QApplication(sys.argv)和APP.exec_(),但不知道如何啓動它。

感謝

回答

0

應該只有的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() 
+0

真棒說做到了! 所以我應該將init_app模塊放在tool.py中設置APP = QtGui.QApplication(sys.argv)的地方? 如果我拿出來它仍然有效,所以它似乎並不需要。僅限於main.py? – ghost654