Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PyQT code can not run twice : Kernel stops #2970

Open
mvkrdndm opened this issue Feb 8, 2016 · 12 comments

Comments

Projects
None yet
@mvkrdndm
Copy link

commented Feb 8, 2016

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = QWidget()
    window.setGeometry(100, 100, 500, 300)
    window.setWindowTitle("PyQT Tuts!")
    window.show()
    sys.exit(app.exec_())  

i can not run the code twice it says kernel stopped.but in other ide's it runs smoothly again and again in web there are other ppl mention about this please fix this bug
http://stackoverflow.com/questions/19459299/running-a-pyqt-application-twice-from-one-prompt-in-spyder
http://stackoverflow.com/questions/33527108/pyqt-examples-not-running-second-time

this bug is general it covers all spyder versions i think ,i tried a few but in all , kernel crashes

@ccordoba12 ccordoba12 added this to the v3.0rc1 milestone Feb 9, 2016

@ccordoba12

This comment has been minimized.

Copy link
Member

commented Feb 9, 2016

Yes, this is a known issue and we don't know how to fix it yet.

@goanpeca

This comment has been minimized.

Copy link
Member

commented Feb 9, 2016

@mvkrdndm, I would advice that you use F6 option Execute in a new dedicated Python console when running Qt Code.

image

@eendebakpt

This comment has been minimized.

Copy link
Contributor

commented Feb 9, 2016

The following code works for me. Note the check on QApplication already running and not executing the exit statement at the end.

import sys
import PyQt4.QtGui as QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

if __name__ == '__main__':
    app = QtGui.QApplication.instance()
    if app is None:
        app = QtGui.QApplication(sys.argv)
    else:
        print('QApplication instance already exists: %s' % str(app))

    window = QWidget()
    window.setGeometry(100, 100, 500, 300)
    window.setWindowTitle("PyQT Tuts!")
    window.show()
    app.exec_()
@cvanelteren

This comment has been minimized.

Copy link

commented Jun 6, 2017

The solution provided by @eendebakp does not work for me on windows, but does on linux
Windows machine uses anaconda w/ python 3.5
Linux machine uses python 3.6 (no anaconda)

@TheHugeManatee

This comment has been minimized.

Copy link

commented Jul 5, 2017

based on @eendebakpt 's code, the following workaround works for me on windows/anaconda 4.4.0/python 3.6:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *


if __name__ == '__main__':
    app = QtWidgets.QApplication.instance()
    if app is None:
        app = QtWidgets.QApplication(sys.argv)
    else:
        print('QApplication instance already exists: %s' % str(app))

    window = QtWidgets.QWidget()
    window.setGeometry(100, 100, 500, 300)
    window.setWindowTitle("PyQT Tuts!")
    window.show()
    app.exec_()

can't say anything about other platforms currently

@ccordoba12 ccordoba12 modified the milestones: v3.2.1, v4.0beta2 Jul 5, 2017

@ccordoba12 ccordoba12 modified the milestones: v3.2.1, v3.2.2 Jul 28, 2017

tentekal added a commit to tentekal/backgroundSubtraction that referenced this issue Aug 2, 2017

@ccordoba12 ccordoba12 modified the milestones: v3.2.2, v3.2.3 Aug 26, 2017

@youtanyouzhen

This comment has been minimized.

Copy link

commented Sep 1, 2017

I get rid of it by running the program with hot key F10 (Run→Profile).

@ccordoba12 ccordoba12 modified the milestones: v3.2.3, v3.2.4 Sep 8, 2017

@ccordoba12 ccordoba12 modified the milestones: v3.2.4, v3.2.5 Sep 29, 2017

@ccordoba12 ccordoba12 modified the milestones: v3.2.5, v3.2.x Nov 30, 2017

@ccordoba12 ccordoba12 modified the milestones: v3.x, Not sorted Oct 11, 2018

@gpgreeff

This comment has been minimized.

Copy link

commented Mar 26, 2019

The workarounds mentioned here did not work for me, but this seems to work for me (Windows, Anaconda, Python 3.7.2, IPython 7.2.0):

app = QtWidgets.QApplication(sys.argv)
app.lastWindowClosed.connect(app.quit)
widget = QtWidgets.QDialog()
widget.show()
app.exec_()
@malekzahedi

This comment has been minimized.

Copy link

commented Apr 11, 2019

The workarounds mentioned here did not work for me, but this seems to work for me (Windows, Anaconda, Python 3.7.2, IPython 7.2.0):

app = QtWidgets.QApplication(sys.argv)
app.lastWindowClosed.connect(app.quit)
widget = QtWidgets.QDialog()
widget.show()
app.exec_()

This works on Windows, Anaconda, Python 3.7,3, IPython 7.4.0

@jcnossen

This comment has been minimized.

Copy link

commented May 4, 2019

Manually deleting the app object works for me (Windows / Python 3.6.7 / Qt5):

app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QDialog()
widget.show()
app.exec_()
del app
@maxstrobel

This comment has been minimized.

Copy link

commented May 5, 2019

The workaround of @jcnossen works also on (Debian Linux / Python 3.6.8 / Qt5.9.6 / IPython 7.5.0 / JupyterLab 0.35.5):

if 'app' in locals():
    del app

app = QtGui.QApplication([])

# CODE

if __name__ == '__main__':
    QtGui.QApplication.instance().exec_()
@ccordoba12

This comment has been minimized.

Copy link
Member

commented May 5, 2019

JupyterLab 0.35.5

I don't understand this. Are you reporting to the right place?

@maxstrobel

This comment has been minimized.

Copy link

commented May 5, 2019

I did not investigate this deeper, but I think this is an issue of running Qt in IPython.
JupyterLab runs internally also an IPython kernel.
So, I am not sure, whether this is an issue of spyder at all...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.