I'm trying to write a pyqt5 application. It throws an exception from the Python side in a slot (i.e. callback) that responds to the user clicking a button. I run the application with
python -m pdb myapp.py
which begins with
# myapp.py
import sys
from PyQt5.QtCore import pyqtRemoveInputHook
pyqtRemoveInputHook()
from PyQt5.QtWidgets import QApplication, QWidget
from views import MainWindow
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
as per this question. I get the debugger prompt at program start and press c to continue. But when the crash happens, I get a stack trace and then a core dump. If I run with ipdb the core dump doesn't happen, but the program locks up before it can drop me into the debugger.
It seems like it's impossible to debug pyqt5 programs if the error is in a callback, although this worked fine in pyqt4 if I recall. How can I get a Python debugger working with pyqt5?
Related
I want to create a PyQt5 program that has the following:
A basic window:
app = QApplication([])
root = QMainWindow()
root.show()
app.exec_()
A terminal that runs a user input and can accept commands such as root.setWindowTitle("PyQt")
or if it were in a class className.setWindowTitle("PyQt").
This terminal must be the one from where you started running the program (xterm, terminus, gnome-terminal ...).
Final note, I have tried the QThread, multiprocess, threading, subprocess libraries to no avail.
When I execute my PyQt script in Spyder, it doesn't seem to do anything except clearing variables. When I execute it again, it works as expected.
As you can see below, I cannot reduce the code any further, but the problem remains unchanged.
Is this the expected behaviour? What am I doing wrong?
import sys
from PyQt5 import QtWidgets
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
window.show()
app.exec_()
In detail:
I open Spyder.
I execute the script. A window opens and I terminate by closing the window (expected behaviour).
I execute the script. After a few seconds, the console returns. Nothing seems to have happened except that the variables are gone (unexpected behaviour).
Repeat from 2...
The problem isn't so much that the variables are being cleared, but rather that the PyQt application cannot be run repeatedly within Spyder. It's a common issue that is addressed in the Spyder Wiki: How to run PyQt applications within Spyder.
It seems to be tied to the fact that Spyder is itself a Qt application. Specifically, the Wiki entry has this by way of an explanation:
The most common problem when running a PyQt application multiple times inside Spyder is that a QApplication instance remains in the namespace of the IPython console kernel after the first run. In other words, when you try to re-run your application, you already have a QApplication instance initialized.
The work-around is to make sure that the QApplication instance goes out of scope when your script exits, i.e., create it within the scope of a function. Using the simple example from above, it just comes down to this:
import sys
from PyQt5 import QtWidgets
def main():
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
window.show()
app.exec_()
main()
I am very new to PyQTt, I am working on Mac OS X, so i have installed PyQtX which works without any problems when importing PyQt4.
So i just tried making a very simple application:
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.show()
window.raise_()
When launching this with Python2, Python application shows in dock and instantly closes (Without showing any result).
Is it because i'm missing something on my system? What can a problem be?
i am trying out pyQT and followed a tutorial to get startet. I am using Spyder on Win7 64 bit and qt4. Everythinginstalled with spyder. I build a GUI with QTDesigner and made a .py file. I am now trying to start the gui as the tutorial says. I get no errors but the code just finishes and nothing shows up.
This is the code i use:
import sys
from PyQt4 import QtCore, QtGui
from qtTestController import Ui_Form
if __name__ == "main":
app = QtGui.QApplication(sys.argv)
ui = Ui_Form()
ui.show()
app.setMainWidget(ui)
app.exec_loop()
If i start it a second time the console says "Reloaded modules: qtTestController"
But still nothing happens. Am I missing anything?
Whenever i execute the code and close the window, it closes,but the python console in the IDE doesn't return the exit code,when i try to run it again i get a warning dialog that says something like
No python console is selected to run main.py
So i have to close the IDE python console, and open a new one, then run the program in the new python console
I'm using spyder IDE 64 bits on windows
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
if __name__ == "__main__":
app = QApplication(sys.argv)
win = QMainWindow()
win.show()
sys.exit(app.exec_())
If you execute the code in a running (i)python console, you do not need to start a qapplication in the usual way, the two lines
win = QMainWindow()
win.show()
will be enough to get you running. This is because the console already has a (threaded) qapplication prepared for you.
The error message can be caused when no console has focus (i.e., perhaps the one you were using quit because of sys.exit(), or you clicked away etc). You need to simply click in an (i)python console to get it to be 'selected', and then the run button should work again.