Search code examples
pythonwindowpyqt4pycharm

Window is closing immediately after I run program in PyQt 4 (anaconda) [PyCharm 4.5]


So, I am trying to run a very simple program (a window) in Pycharm that is running anaconda 2.7 & PyQt4. Whenever I click the Run button it opens my program but closes the window too fast for me to even see it. May anyone, please help? Thank you!

P.S.

I'm very new to programming.

{__author__ = 'Jay'

import sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)

window = QtGui.QWidget()

window.show()}

Solution

  • You need to block the execution of the program after you call window.show() so that the window object remains active otherwise it will be garbage collected. app.exec_() does this for you.

    {__author__ = 'Jay'
    
    import sys
    from PyQt4 import QtGui
    
    app = QtGui.QApplication(sys.argv)
    
    window = QtGui.QWidget()
    
    window.show()
    app.exec_()}     # added this line