pyqt gui not showing - python

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?

Related

PyInstaller app keeps opening itself while using pyQode

Problem: The following program is keep starting itself every 5 seconds after making it executable using PyInstaller. It keeps running itself again and again. The code works fine without making if I run it directly using python.
I spent more than a day to fix this issue but no luck so far.
I tried it on Windows and Mac. Python version 3.7, PyInstaller Version 3.5 & 3.6.
PyInstaller Command:
pyinstaller --onefile generic_code_edit.py
Python Code:
import logging
logging.basicConfig(level=logging.DEBUG)
import sys
from pyqode.qt import QtWidgets
from pyqode.core.widgets import GenericCodeEdit
def main():
app = QtWidgets.QApplication(sys.argv)
# create editor and window
window = QtWidgets.QMainWindow()
editor = GenericCodeEdit()
# open a file
editor.file.open(__file__)
window.setCentralWidget(editor)
# run
window.show()
app.exec_()
editor.file.close()
if __name__ == "__main__":
main()
The Debug log:
DEBUG:pyqode.core.cache:getting encoding for generic_code_edit.py
DEBUG:pyqode.core.managers.file:mimetype detected: text/x-python
DEBUG:pyqode.core.managers.file:file open: generic_code_edit.py
ERROR:pyqode.backend:DEBUG:pyqode.qt:importing PyQt5
ERROR:pyqode.backend:DEBUG:pyqode.qt:imported PyQt5
ERROR:pyqode.backend:INFO:pyqode.qt:using pyqt5
ERROR:pyqode.backend:DEBUG:pyqode.core.cache:getting encoding for
generic_code_edit.py
ERROR:pyqode.backend:DEBUG:pyqode.core.managers.file:mimetype
detected: text/x-python
ERROR:pyqode.backend:DEBUG:pyqode.core.managers.file:file open:
generic_code_edit.py
Update:
Finally I found the problem. It is related to the autocomplete feature. This feature does not load perfectly with pyinstaller and cause a new start. Although I could not be able to make it working with pyinstaller, but I have been able to stop these feature to solve the starting loop issue.
I have passed an empty server file as backend to solve the issue. But still I want to make the Autocomplete working with pyinstaller.
editor = GenericCodeEdit(None, 'empty_file.py')

How do I stop Spyder from clearing variables instead of executing my PyQt script?

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()

pyqt5 application won't drop into a debugger - just crashes

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?

(PyQT4) Python instantly closes after launch

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?

PyQt app doesn't exit when i close the window

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.

Categories