I am learning GUI programming in python. I am using Pyside framework. For practice I am developing one simple application where user open the new window button is clicked. I have developed simple prototype for the same as:
MainFile.py
from PySide import QtGui, QtCore
import Starter
import sys
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
starter = Starter.StartClass()
starter.show()
sys.exit(app.exec_())
Starter.py
from PySide import QtGui, QtCore
import start1_ui, pop_up
class StartClass(start1_ui.Ui_MainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.click_button.clicked.connect(self.call_method)
def call_method(self):
print 'hey! I am Starter'
pop = pop_up.PopupClass()
pop.setAttribute(QtCore.Qt.WA_DeleteOnClose)
pop.show()
pop_up.py
from PySide import QtGui, QtCore
import popup_ui
class PopupClass(popup_ui.Ui_MainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
#self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
print 'I am shown'
I have converted the *.ui files created in qtDesigner to *.py using pyside-uic.I am not putting these py files, as these are too large.Structure of the files as
start1_ui.py
from PySide import QtCore, QtGui
class Ui_MainWindow(QtGui.QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(437, 290)
## Much more code for setting various widget attribute as width. height
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.click_button.setText(QtGui.QApplication.translate("MainWindow", "ClickMe", None, QtGui.QApplication.UnicodeUTF8))
and popup_ui.py
class Ui_MainWindow(QtGui.QMainWindow):
def setupUi(self, MainWindow):
#setting up widgets here
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("MainWindow", "Finally, I am opened", None, QtGui.QApplication.UnicodeUTF8))
Now when I clicked button in starter.py, new popup opens but closes immediately. I have tried using attribute as QtCore.Qt.WA_DeleteOnClose
but it didn't helped?
Where am I missing? How can open popup and make it visible in above code?
EDIT: From the comments, we need to maintain the reference of object of new window so that gc will not clean it. Then how come first windows remains as it is after we ran the code?
By adding it in the list for making the reference, it will create multiple windows. I need only window to created. And also f parent windows is closed then automatically closes the popup window.
How can I use QtCore.Qt.WA_DeleteOnClose
method in my above code?