-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 cannot run twice #2970
Comments
Yes, this is a known issue and we don't know how to fix it yet. |
@mvkrdndm, I would advice that you use |
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_() |
The solution provided by @eendebakp does not work for me on windows, but does on linux |
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 |
Thanks to @eendebakpt and @TheHugeManatee at spyder-ide/spyder#2970
I get rid of it by running the program with hot key F10 (Run→Profile). |
@dalthviz, can you reproduce this on Windows? |
@ccordoba12 testing this on Windows, Python 3.7.3/Anaconda: Code versions that work: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_() # Similar code to the original post but using PyQt5
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication, QWidget
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_()) Code version with strange behaviors:Running the following code from a file no issue raises but seems like the kernel is restarting when running the second time: import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setGeometry(100, 100, 500, 300)
window.setWindowTitle("PyQT Tuts!")
window.show()
app.exec_() Also, should the wiki page we have about running PyQt applications be updated? |
@dalthviz, so what's your conclusion? I mean, when do things work and when do they fail? |
I can reproduce if I paste the code in the console directly. I get a segfault when creating the widget:
Without the widget: In [1]: from PyQt5 import QtWidgets
...: app = QtWidgets.QApplication([])
...: app.exec_()
Out[1]: 0
In [2]: from PyQt5 import QtWidgets
...: app = QtWidgets.QApplication([])
...: app.exec_()
QApplication::exec: Please instantiate the QApplication object first
Out[2]: -1
In [3]: Without executing: (Crashes)
Without an app: (Crashes) In [1]: from PyQt5 import QtWidgets
...: window = QtWidgets.QWidget()
...: window.show()
In [1]: But I can reproduce that in qtconsole and in ipython and in python:
Some tests in python: # Crashes after two times with SEGV
from PyQt5 import QtWidgets; app = QtWidgets.QApplication([]); QtWidgets.QWidget()
# Doesn't crash
from PyQt5 import QtWidgets; QtWidgets.QApplication([]); QtWidgets.QWidget()
# Crashes immediately with ABRT
from PyQt5 import QtWidgets; QtWidgets.QWidget()
# Doesn't crash
from PyQt5 import QtWidgets; app = QtWidgets.QApplication([])
# Doesn't crash
from PyQt5 import QtWidgets; app = QtWidgets.QApplication([]); QtWidgets.QWidget(); del app |
My guess is the following: when
But the new python object is really a wrapper around the same C++ object. Therefore the second wrapper ends up empty. |
Could you try spyder-ide/spyder-kernels#189 to see if this fixes it? |
Will take a look @impact27 thanks! |
@impact27 I left a review at spyder-ide/spyder-kernels#189 . Thanks! :) |
@dalthviz does it work for you with the fix? |
Works like a charm on Mac @impact27 ! # Standard library imports
import sys
# Third party imports
from qtpy.QtWidgets import QApplication, QWidget
if __name__ == '__main__':
app = QApplication([])
window = QWidget()
window.setGeometry(100, 100, 500, 300)
window.setWindowTitle("PyQT Tuts!")
window.show()
window.raise_()
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
The text was updated successfully, but these errors were encountered: