0

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?

ajay_t
  • 2,347
  • 7
  • 37
  • 62
  • 1
    possible duplicate of [PyQt window closes after launch](http://stackoverflow.com/questions/18061178/pyqt-window-closes-after-launch) – three_pineapples Feb 02 '15 at 21:51
  • You did not store a reference and garbage collection in Python immediately destroyed the window again. – NoDataDumpNoContribution Feb 03 '15 at 09:38
  • @ three_pineapples, I have looked those questions, you mentioned. But In the answers which are given, then can open multiple windows. Also if I close the parent window i.e. Starter, then also Popup remains open. Only one Popup need to be allowed and when I close the starter windows then opened popup window should get closed – ajay_t Feb 03 '15 at 14:16
  • Well that's probably another question then. Your current problem is caused by not storing a reference to the new window when the new window has no parent. If you want specific close behaviour, and can't work it out yourself, I would suggest posting a new question. – three_pineapples Feb 04 '15 at 07:46

1 Answers1

0

Maybe try setting the parent of the popup to be main window? In starter.py, try pop.show(self) instead of pop.show().

Becca codes
  • 542
  • 1
  • 4
  • 14
  • there is no show() method which accepts the arguments – ajay_t Feb 06 '15 at 07:48
  • My goodness, I must have been tired when I wrote that answer. I meant to say `pop = pop_up.PopupClass(self)`. Though from the other comments that have since appeared, perhaps that is not what you want either. – Becca codes Feb 11 '15 at 15:55