smilingbuddha
smilingbuddha

Reputation: 14660

GUI window in Python (PyQT) flashing and closing down immediately?

I am new to PyQT and I have just started learning about it through this video: https://www.youtube.com/watch?v=JBME1ZyHiP8

When I ran the code on my Ubuntu 14.04

import sys
from PyQt4 import QtGui # Always have these two imports

app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.setGeometry(50,50,500,300)
window.setWindowTitle("PyQt start")

window.show()

The window crated just flashes and closes down. How do I get the window to stay so that I can interact with it? The code in the Youtube video above demonstrated it on a Windows platform. Do I have to append anything Ubuntu specific to my code?

Upvotes: 3

Views: 2106

Answers (1)

101
101

Reputation: 8979

You aren't running the app, add this line to the end:

sys.exit(app.exec_())

From the relevant documentation:

int QApplication.exec_ ()

Enters the main event loop and waits until exit() is called, then returns the value that was set to exit() (which is 0 if exit() is called via quit()).

It is necessary to call this function to start event handling. The main event loop receives events from the window system and dispatches these to the application widgets.

Generally, no user interaction can take place before calling exec(). As a special case, modal widgets like QMessageBox can be used before calling exec(), because modal widgets call exec() to start a local event loop.

Upvotes: 2

Related Questions