I am trying to create a UI that will act as a hub to launch all the other tools that have been created. The problem is that if i try to launch the UI from the toolshub UI, it won't let me because the event loop is already running. I know that I cannot do this APP = QtGui.QApplication(sys.argv) and APP.exec_() when launching the new window because the event loop is already running when I did that for the toolshub UI. But I can't figure out how to do it another way.
Here is an example code for one of the tools, works launching on its own.
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
Now here is the code for the toolshub ui. These are both separate scripts. In toolshub i am importing the tool above.
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()
So how do write and structure these so that I can open the tool UI from the toolshub UI? I am guessing the code i have to change is the tool UI, I know i have to take out APP = QtGui.QApplication(sys.argv) and APP.exec_(), but not sure what to do to launch it.
Thanks
There should only be one instance of QApplication and this should be created before creating any widget, so it is unnecessary to instantiate it in each module.
In order for the QDialog to be displayed properly you must execute its exec_()
tool.py:
[...]
def start_ui():
win= toolwindow()
win.exec_()
complete code:
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.