1
votes

Upon calling the show method on simple the simple window does not show. Why doesn't my Simple window show. :(

import sys
from PyQt4 import QtGui


class Widget(QtGui.QWidget):

    def __init__(self):
        super(Widget, self).__init__()
        simple = Simple()

        button = QtGui.QPushButton("Button", self)
        button.clicked.connect(simple.show)
        self.show()


class Simple(QtGui.QWidget):

    def __init__(self):
        super(Simple, self).__init__()
        self.setGeometry(300, 250, 250, 150)
        self.setWindowTitle("Simple Widget")


if __name__ =="__main__":
    app = QtGui.QApplication(sys.argv)
    widget = Widget()
    sys.exit(app.exec_())

Please Help!

1
where have you called show for Simple widget ? - Abhishek Bansal
When the button is clicked - user3652082

1 Answers

8
votes

The problem with your code is that, simple in __init__ method of class Widget is a local variable, so as soon as the __init__ method finishes execution, the simple object is destroyed by the python Garbage Collector, thus the window does not appear because the object does not exist in the memory. To solve your problem, just add self at the starting of the simple variable to make it member variable.

...
self.simple = Simple()
button = QtGui.QPushButton("Button", self)
button.clicked.connect(self.simple.show)
...