arthaigo
arthaigo

Reputation: 453

Python Kernel crashes after closing an PyQt4 Gui Application

Ok here is my problem:

I want to create a PyQt4 Gui, which can be executed from a python console (tested with IDLE, Spyder Console and IPython Console) and then allows the user to change and view variables. After closing the app the user should be able to do further work with the variables in the console. But by closing the Gui the Kernel crashes and it is not possible to make any new input to the console.

I'm working with Python 2.7 and PyQt4. I am using the following code to start an close the application:

app=QtGui.QApplication(sys.argv)
MainApp=plottest()
MainApp.show()
sys.exit(app.exec_())

Upvotes: 8

Views: 6643

Answers (6)

WilsonWan
WilsonWan

Reputation: 124

hdunn's answer worked for me. Have deleted sys.exit() to avoid exiting the application totally. Performing the first step alone, as suggested by others in github forum, solved part of the problem but window never showed up, nor any error message. Performing the second part solved everything like a charm! Thans, hdunn!

# Check if there's a pre-existing QApplication instance 
# If there is, use it. If there isn't, create a new one.
app = QtGui.QApplication.instance()
if not app:
    app = QtGui.QApplication(sys.argv)

# Ensure that the app is deleted when we close it
app.aboutToQuit.connect(app.deleteLater)

# Execute the application
MainApp = plottest()
MainApp.show()
app.exec_()

Upvotes: 1

hdunn
hdunn

Reputation: 91

What you need to do is:

  1. Check that there isn't already a QApplication instance when trying to create a new one
  2. Ensure that the QApplication object is deleted after it's been closed

(See simple IPython example raises exception on sys.exit())

# Check if there's a pre-existing QApplication instance 
# If there is, use it. If there isn't, create a new one.
app = QtGui.QApplication.instance()
if not app:
    app = QtGui.QApplication(sys.argv)

# Ensure that the app is deleted when we close it
app.aboutToQuit.connect(app.deleteLater)

# Execute the application
MainApp = plottest()
MainApp.show()
sys.exit(app.exec_())

Using this code you can rerun the application as many times as you want in IPython, or anywhere else. Every time you close your Qt application, the app object will be deleted in python.

Upvotes: 3

Arun Sooraj
Arun Sooraj

Reputation: 737

I still don't know the issue and solution. None of the above worked for me. Currently I am coding in the spyder GUI and running on "cmd" by giving 'python' command. In CMD, it is working fine.

Update the solution if I get it. :-)

Upvotes: 1

Gabriel Asqui
Gabriel Asqui

Reputation: 339

The easy solution here https://www.reddit.com/r/learnpython/comments/45h05k/solved_kernel_crashing_when_closing_gui_spyder/

only put

if __name__ == "__main__":
    app=0           #This is the solution
    app = QtGui.QApplication(sys.argv)
    MainApp = Dice_Roller()
    MainApp.show()
    sys.exit(app.exec_())

Upvotes: 6

Marcin Łęcki
Marcin Łęcki

Reputation: 11

I think that your problem is that python console closes (its not kernel crash). For example in Spyder python icon on top of console window becomes grey and you cant do anything besides running another console.

Anyway, you should write app.exec() instead of sys.exit(app.exec()).

I believe sys.exit(app.exec()) passes exit code to console and closes it. Using simple app.exec() won't close console.

Concluding your code should look like:

app=QtGui.QApplication(sys.argv)
MainApp=plottest()
MainApp.show()
app.exec_() 

I hope it helps.

Upvotes: 1

user3413108
user3413108

Reputation:

I do not think you mean a kernel crash. Rather, I think you are talking about exiting the python console. This is caused by sys.exit(app.exec_()). For example try (for example in spyder) the following two codes:

import sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)

widget = QtGui.QWidget()
widget.setWindowTitle('simple')
widget.show()

#sys.exit(app.exec_())

Here you should get an empty window and the python console will stay alive. The second one, where sys.exit(app.exec_()) is included, will exit the python console at the end and the window disappears:

import sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)

widget = QtGui.QWidget()
widget.setWindowTitle('simple')
widget.show()

sys.exit(app.exec_())

I hope this helps.

Upvotes: 1

Related Questions