Can't exit PyQt application on 3.7.4 and Spyder - python-3.x

Running on Windows 10 64 bit and Python 3.7.4 (no Anaconda), the basic script from Terminate PyQt Application
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5.QtCore import QSize
class HelloWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(640, 480))
self.setWindowTitle("Hello world")
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
gridLayout = QGridLayout(self)
centralWidget.setLayout(gridLayout)
title = QLabel("Hello World from PyQt", self)
title.setAlignment(QtCore.Qt.AlignCenter)
gridLayout.addWidget(title, 0, 0)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = HelloWindow()
mainWin.show()
sys.exit( app.exec_() )
does not exit when starting from Spyder.
The IPython console simply "idles", but never returns. Running the script from the command prompt (i.e. 'python script.py') works OK.
Switching back to a fresh install of Python 3.7.2, everthing works as expected. The IPython version is in both cases 7.7.0.
In both cases (3.7.2 and 3.7.4) all modules including Spyder had been installed via pip.
Any idea why that is? Is it a Spyder related issue?

Keeping the Graphics Backend as not "Inline" is creating this problem.
Either change the graphics backend as,
Tools -> Preferences --> IPython Console -> Graphics --> Graphics Backend as Inline
Or Restore the default settings of the Spyder Environment. Which can be done by,
Tools -> Preference --> Reset to Defaults.

Related

PyQt5 layout.addWidget will not accept pyqtgraph widget [duplicate]

This question already has an answer here:
Adding pyqtgraph to PyQt6
(1 answer)
Closed 4 days ago.
I am trying to add a GraphicsLayoutWidget from pyqtgraph to a PyQt application. On multiple machines, the code below works without error: I see a QLineEdit input textbox above a black square.
import pyqtgraph as pg
from PyQt5.QtWidgets import QWidget, QLineEdit, QVBoxLayout, QLabel, QApplication
import sys
class Test(QWidget):
def __init__(self, headers: list = None, parent=None):
QWidget.__init__(self, parent=parent)
layout = QVBoxLayout(self)
layout.addWidget(QLabel('QLineEdit'))
w = QLineEdit()
print(type(w))
layout.addWidget(w)
x = pg.GraphicsLayoutWidget()
print(type(x))
layout.addWidget(x)
if __name__ == "__main__":
app = QApplication(sys.argv)
main = Test()
main.show()
app.exec()
However, on one machine, I receive the error below and the code will not run.
File "my_file.py", line 22, in __init__
layout.addWidget(self, w)
TypeError: addWidget(self, a0: QWidget, stretch: int = 0, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'GraphicsLayoutWidget'
I've tried adding a self.setLayout(layout), but this doesn't fix the issue.
I updated both machines so that their installed packages and versions both match. On the non-working machine, I tried uninstalling pyqtgraph and PyQt5, then reinstalling them with PyQt5 first followed by pyqtgraph. The error persists.
On both machines I am running:
Python 3.11.1
PyQt5 5.15.8
pyqtgraph 0.13.1
All other packages that I see listed by pip freeze or pip list are identical in version, i.e. the lists contain the same packages and the same versions of each package.
The print lines in the above code print the class of each object.
For QLineEdit: <class 'PyQt5.QtWidgets.QLineEdit'>.
For GraphicsLayoutWidget: <class 'pyqtgraph.widgets.GraphicsLayoutWidget.GraphicsLayoutWidget'>.
I've run the code in PyCharm as well as from the command line and the result is the same. For some reason, it seems that PyQt5 is not identifying widgets from pyqtgraph.widgets as QWidget.
I don't even know how to reproduce the error on the working machines.
I'm not sure where to go from here.
You may have to specify the stretch.
Can you run this example?
Also I've normally used pyqt.PlotWidget for general graphing with no problems.

QsciScintilla on Pyqt4 for python35 error (unofficial whl)

I want to use pyqt4 on python35. However, the latest official release supports py=<34
So I found this university website https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyqt4
which unofficially extended support for later versions including python35
My problem is that QsciScintilla is not working. I tried to run this code:
import sys
from PyQt4.QtGui import QApplication
from PyQt4 import QtCore, QtGui, Qsci
from PyQt4.Qsci import QsciScintilla
if __name__ == "__main__":
app = QApplication(sys.argv)
editor = QsciScintilla()
editor.show()
sys.exit(app.exec_())
The window becomes unresponsive then crashes:
Process finished with exit code -1073740771 (0xC000041D)
The reason I want to use pyqt4 on python3.5 is that I have a quit big application built on pyqt4
and I want to upgrade python version of the project to 35
I moved to PyQt5. It is always not a good idea to use an unofficial package.

How to change the source of python used by atom runner or script in Atom?

I want to run python code with Kivy by atom-runner or script, but when I run the simple any codes with Kivy, I got ModuleNotFoundError. I checked python path by
import sys
print(sys.prefix)
which prints ''/Library/Frameworks/Python.framework/Versions/3.7''
I guess I need to use Anaconda's python because I can run Kivy app on terminal and terminal uses python3.7 showing Anaconda.inc things.
But I can not find where to write path of python in Atom.
I tried pip install Kivy and it worked but nothing changed in Atom. And Kivy3 is in Application folder.
Just in case, my Kivy codes is this
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
class IntroKivy(App):
def build(self):
return Button(text="Hello, World!")
if __name__ == "__main__":
IntroKivy().run()
It will be perfect if I can run this codes without using terminal as it is faster and quick.
Best

PyQt5 howto load UI file in bundle

I'm trying to load a Qt5 file in a bundle on a Mac.
#!/usr/bin/env python
from PyQt5.QtWidgets import (QApplication, QMainWindow)
from PyQt5 import uic
import sys
import os
MainUI = os.path.dirname(os.path.realpath(__file__)) + "/data/MainUI.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(MainUI)
class MyForm(QMainWindow,Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyForm()
w.show()
sys.exit(app.exec_())
This works fine in the commandline but when I bundle this with pyinstaller the program aborts because it can't find the file mainUI.ui. I found this solution but I can't get it working (resource_path function not found).
I've made another and that worked fine but this one (a one file program) I can't get working.
Edit:
I stopped trying to fix this and went for the multiple file solution: 1 file to startup and one for the PyQt functions. That works 100%.

.show() and/or.exec() from Pyqt5 not working in Spyder

I have Python 3.6.3 and Spyder 3.2.4, both installed along with Pyqt5 yesterday on a computer with a new installation of Windows 10. I am having a problem running some previously written Pyqt5 code in Spyder. The below code shows a minimum code snippet that reproduces the problem.
The Spyder IPython console hangs indefinitely on running the code, and never opens the window. Ctrl-C does not stop execution, so I have to kill Spyder. If I try to run it line by line, I observe that "w.show()" executes but does nothing, while "app.exec()" is the line that hangs. In contrast, if I instead run it from the command line console, "w.show()" makes a nonfunctional window appear, while "app.exec()" makes the window functional. All of the non-pyqt code I have run in Spyder executes just fine.
I'd prefer to develop code in Spyder because running code from the command line often takes 30-60s to start (presumably from the imports.)
I could run the code in Spyder on my old (now broken) system, so clearly it is a problem with my installation or computer. But I am at a loss as to what. I have already tried disabling my virus software, updating all of the relevant packages with pip, resetting Spyder from the Anaconda prompt, and restarting the computer. Any other ideas?
import sys
from PyQt5.QtWidgets import QApplication, QWidget
if not QApplication.instance():
app = QApplication(sys.argv)
else:
app = QApplication.instance()
print("app started")
w = QWidget()
w.resize(250, 250)
w.move(200, 200)
w.setWindowTitle('Test window')
print('app setting')
w.show()
print("shown")
app.exec_()
#sys.exit(app.exec_())
print("exit")
When running this in python it needs to be modified as below. I've modified your script a little to be able to let it run for you some of the main PyQt conventions. See also my inline comments to make sense of what you're trying to accomplish with your print lines. Enjoy!
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget
class MyMainUI(QtWidgets.QMainWindow):
def __init__(self, parent=None):
print ' Start Main'
super(MyMainUI, self).__init__(parent)
self.setWindowTitle('Test window')
self.resize(250, 250)
self.move(200, 200)
MySpidercodeHook()
def MySpiderCodeHook(self):
...link/run code...
if __name__ == '__main__':
app = QApplication(sys.argv) # no QApplication.instance() > returns -1 or error.
# Your setting up something that cannot yet be peeked
# into at this stage.
print("app started")
w = MyMainUI() # address your main GUI application
# not solely a widgetname..that is confusing...
x = 200
y = 200
# w.move(x, y) # normally this setting is done within the mainUI.
# See also PyQT conventions.
print 'app setting: moving window to position : %s' % ([x, y])
w.show()
print("shown")
app.exec_()
#sys.exit(app.exec_()) # if active then print "exit" is not shown..
# now is while using app.exec_()
#sys.exit(-1) # will returns you '-1' value as handle. Can be any integer.
print("exit")
sys.stdout.flush() # line used to print "printing text" when you're in an IDE
# like Komodo EDIT.

Resources