If the function creates a PyQt object that the application should continue to use, you will need to make sure that the link to it is stored somehow. Otherwise, it can be removed by the Python garbage collector immediately after the function returns.
Thus, either give the object a parent object, or save it as an attribute of some other object. (In principle, an object could also be made a global variable, but this is usually considered bad practice).
Here is a revised version of your sample script that demonstrates how to fix your problem:
from PySide import QtGui, QtCore class Window(QtGui.QMainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) menu = self.menuBar().addMenu(self.tr('View')) action = menu.addAction(self.tr('New Window')) action.triggered.connect(self.handleNewWindow) def handleNewWindow(self): window = QtGui.QMainWindow(self) window.setAttribute(QtCore.Qt.WA_DeleteOnClose) window.setWindowTitle(self.tr('New Window')) window.show()
source share