Python core crashes after closing PyQt4 Gui application

Ok, here is my problem:

I want to create a PyQt4 Gui that can be executed from the python console (tested using IDLE, Spyder Console and IPython Console), and then allows the user to modify and view the variables. After closing the application, the user should be able to continue working with variables in the console. But by closing Gui, the kernel crashes, and it is impossible to make a new entry to the console.

I work with Python 2.7 and PyQt4. To run the application, I use the following code:

app=QtGui.QApplication(sys.argv) MainApp=plottest() MainApp.show() sys.exit(app.exec_()) 
+7
source share
5 answers

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

only places

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

What you need to do is:

  1. Verify that there is no longer a QApplication instance when trying to create a new
  2. Make sure the QApplication object is deleted after it is closed

(See A Simple Example. IPython throws an exception for sys.exit () )

 # Check if there 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 restart the application as many times as you want in IPython or elsewhere. Each time you close your Qt application, the application object will be deleted in python.

+2
source

I don’t think you mean the core crash. Rather, I think you're talking about exiting the python console. This is called 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 a blank window and the python console will stay alive. The second, where sys.exit(app.exec_()) enabled, will exit the python console at the end and the window will disappear:

 import sys from PyQt4 import QtGui app = QtGui.QApplication(sys.argv) widget = QtGui.QWidget() widget.setWindowTitle('simple') widget.show() sys.exit(app.exec_()) 

Hope this helps.

+1
source

I still do not know the problems and solutions. None of this helped me. I am currently coding the spyder GUI and running on "cmd", providing the command "python". In CMD, it works fine.

Update the solution if I get it. :-)

+1
source

I think your problem is that the python console is closing (this is not a kernel failure). For example, in Spyder Python, the icon at the top of the console window turns gray, and you can do nothing but execute another console.

In any case, you should write app.exec() instead of sys.exit(app.exec()) .

I believe sys.exit(app.exec()) passes the sys.exit(app.exec()) code to the console and closes it. Using simple app.exec() does not close the console.

Your code completion should look like this:

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

Hope this helps.

0
source

Source: https://habr.com/ru/post/970320/


All Articles