Reputation: 831
Can someone explain to me the difference between the following two code examples? Why is the top one not working? It executes without error, but the window doesn't stay open.
from PyQt4 import QtGui
import sys
app = QtGui.QApplication(sys.argv)
QtGui.QMainWindow().show()
app.exec_()
and:
from PyQt4 import QtGui
import sys
app = QtGui.QApplication(sys.argv)
win = QtGui.QMainWindow()
win.show()
app.exec_()
Upvotes: 1
Views: 242
Reputation: 2967
In QtGui.QMainWindow().show()
you are creating an object of QMainWindow
and you are showing it. But you do no save that instance of the QMainWindow
in your memory. So eventually python's garbage collection deletes that instance and your QMainWindow
no longer shows.
In the second code: win = QtGui.QMainWindow()
you save the object instance of QMainWindow
to win
in your memory. Python does not consider that as garbage because it is in use and hence your window stays open
Upvotes: 2