pyqtgraph exporting from API within PyQt4 widget fails / crashes python - python

I have a small application that I have built using PyQt4 and pyqtgraph. I want to put a few buttons in that call the exporters available with pyqtgraph (rather than, or really in addition to, using the context menu that pops up when a user right clicks on a plot).
So far, however, I have not been able to get this to work.
Here is a simplified version of the application:
from PyQt4 import QtCore, QtGui
import pyqtgraph as pg
import pyqtgraph.exporters
import numpy as np
import sys
class SimpleUI(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.resize(1500, 1000)
self.plot_widget = pg.GraphicsLayoutWidget(self)
self.layout = QtGui.QVBoxLayout(self)
data = np.arange(10)
self.plt = self.plot_widget.addPlot()
self.plt.plot(data)
self.export_btn = QtGui.QPushButton("Export")
self.export_btn.clicked.connect(self.export)
self.layout.addWidget(self.plot_widget)
self.layout.addWidget(self.export_btn)
def export(self):
img = pg.exporters.ImageExporter(self.plt)
img.export()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = SimpleUi()
ex.show()
sys.exit(app.exec_())
Clicking on the "Export" button in this case causes a dialog to quickly pop up and then disappear.
If instead I put
img.export(copy=True)
And try to paste what's on the clipboard into something (Paint, whatever), python.exe crashes.
Oddly, exporting through the context menu that is available by default with pyqtgraph works just fine. Also, just working in the terminal I can copy / save plotItems just fine using the same exact lines of code as above. I.e.:
import numpy as np
import pyqtgraph as pg
import pyqtgraph.exporters
plt = pg.plot(np.arange(10))
img = pg.exporters.ImageExporter(plt.plotItem)
img.export()
Which implies to me that that exporters are working fine, but there is some weird interaction that is going on when they are called from within a pyqt4 widget in the manner that I am calling them.
I have tried both pyqtgraph 0.9.8 as well as the main branch on github. Very much at a loss as to what is the issue here
Thanks

It looks like you are not storing img anywhere, so it is collected as soon as the call to export() returns.
Explanation:
Objects in Python are kept in memory only as long as they are needed. When Python determines that an object is no longer needed, it deletes the object.
How does Python know when an object is no longer needed? By counting references. When you execute img = ImageExporter(...), a new object is created with one reference: the local variable img.
Variables that are created inside a function are considered local to the scope of that function. When the function exits, the variable img disappears, which causes the reference count of the ImageExporter object to drop to 0, which causes Python to delete the object.
By setting self.img = ImageExporter(...), you are assigning a reference to the object that is not local to the scope of the function (because the SimpleUI object referred to as self continues to exist after the function returns). This allows the object to persist as long as the SimpleUI still holds the reference.

Related

PyQt5 MainWindow with flag instantly goes out of scope

I created UI using Qt Designer. Then I converted the ui file to a .py file (pyuic -x) - it works fine if launched directly. Then I tried to subclass my ui in a separate file to implement additional logic. And this is where things start to go wrong. Inheriting from QMainWindow and my Qt Designer file works OK with no issues, as expected. However, the moment I set any WindowFlag for my QMainWindow (any flag - I tried these: StaysOnTop, FramelessWindowHint) and run the file, the window appears and instantly disappears. The program continues to run in a console window, or as a PyCharm process, but the window is gone. It looks to me like it is getting out of scope - but why setting a simple flag would make any difference to the garbage collector? Could someone explain this behaviour?
Minimum code required to reproduce this phenomenon:
from ui import Ui_MainWindow
from PyQt5 import QtCore, QtWidgets, QtGui
import sys
class Logic(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setupUi(self)
self.show()
# self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
# self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_NoSystemBackground, True)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Logic()
sys.exit(app.exec_())
The window should appear and stay on the screen until one (or more) of the flags are uncommented. I use Python 3.8 (32-bit) with PyQt5. Run environment provided by PyCharm. Windows 10.
From the documentation of setWindowFlags():
Note: This function calls setParent() when changing the flags for a window, causing the widget to be hidden. You must call show() to make the widget visible again..
So, just move self.show() after setting the flags, or call it from outside the __init__ (after the instance is created), which is the most common and suggested way to do so, as it's considered good practice to show a widget only after it has been instanciated.

How to display an SVG image in Python

I was following this tutorial on how to write a chess program in Python.
It uses the python-chess engine. The functions from that engine apparently return SVG data, that could be used to display a chessboard.
Code from the tutorial:
import chess
import chess.svg
from IPython.display import SVG
board = chess.Board()
SVG(chess.svg.board(board=board,size=400))
but when I run that code, all I see is a line in the terminal and no image.
<IPython.core.display.SVG object>
The tutorial makes a passing reference to Jupyter Notebooks and how they can be used to display SVG images. I have no experience with Jupyter Notebooks and even though I installed the package from pip and I dabbled a little into how to use it, I couldn't make much progress with regards to my original chessboard problem. But what I do have, is, experience with Qt development using C++ and since Qt has Python bindings, I decided to use those bindings.
Here is what I wrote:
import sys
import chess
import chess.svg
from PyQt5 import QtGui, QtSvg
from PyQt5.QtWidgets import QApplication
from IPython.display import SVG, display
app = QApplication(sys.argv);
board = chess.Board();
svgWidget = QtSvg.QSvgWidget(chess.svg.board(board=board, size=400));
#svgWidget.setGeometry(50,50,759,668)
svgWidget.show()
sys.exit(app.exec_())
A Qt window opens and shows nothing and in the terminal I see a lot of text - (apparently the SVG data is ending up in the console and not in the Qt window that is opening?).
I figured I have to install some SVG library under python so I installed drawSvg from pip. But it seems that library generates SVG images. And was of no use for me.
What is even more strange is, after seeing this SO question, I tried the following:
import sys
import chess
import chess.svg
from PyQt5 import QtGui, QtSvg
from PyQt5.QtWidgets import QApplication
from IPython.display import SVG, display
app = QApplication(sys.argv);
board = chess.Board();
svgWidget = QtSvg.QSvgWidget('d:\projects\python_chess\Zeichen_123.svg');
#svgWidget.setGeometry(50,50,759,668)
svgWidget.show()
sys.exit(app.exec_())
And it showed an image - an SVG image! What is the difference then between my case and this case?
Question: So my question is, what I am doing wrong in the case of the chessboard SVG data? Is the SVG data generated by the python-chess library not compatible with QtSvg?
I think you are getting confused by the scripting nature of Python. You say, you have experience with Qt development under C++. Wouldn't you create a main window widget there first and add to it your SVG widget within which you would call or load SVG data?
I would rewrite your code something like this.
import chess
import chess.svg
from PyQt5.QtSvg import QSvgWidget
from PyQt5.QtWidgets import QApplication, QWidget
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 1100, 1100)
self.widgetSvg = QSvgWidget(parent=self)
self.widgetSvg.setGeometry(10, 10, 1080, 1080)
self.chessboard = chess.Board()
self.chessboardSvg = chess.svg.board(self.chessboard).encode("UTF-8")
self.widgetSvg.load(self.chessboardSvg)
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
EDIT
It would be even better if you would add a paint function to the MainWindow class. Because for sure in future, you would want to repaint your board image many times, whenever you would move a piece. So I would do something like this.
def paintEvent(self, event):
self.chessboardSvg = chess.svg.board(self.chessboard).encode("UTF-8")
self.widgetSvg.load(self.chessboardSvg)
this is how i display SVG image with python, no need third party to do that.
First i save SVG image on disk
second i just call it like os.Startfile("exemple.svg")
Exemple:
boardsvg = svg.board(board(fen), size=600, coordinates=True)
with open('temp.svg', 'w') as outputfile:
outputfile.write(boardsvg)
time.sleep(0.1)
os.startfile('temp.svg')
that's it !

QMainWindow unexpectedly closes when I print a pandas DataFrame?

Below is a minimal example that reproduces this problem.
from PyQt5 import QtWidgets
import pandas as pd
class PandasGUI(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
# This ensures there is always a reference to this widget and it doesn't get garbage collected
self._ref = self
inner = pd.DataFrame([3, 4, 5])
# Printing the DataFrame makes some windows close/disappear leaving only 3 QMainWindow windows
# Commenting out this print statement causes 5 windows to be shown as expected
print(inner)
self.show()
# Should create 5 PandasGUI windows
app = QtWidgets.QApplication([])
for i in range(5):
outer = pd.DataFrame([1, 2, 3])
PandasGUI()
app.exec_()
The problem I have is when I run this code I get 3 or 4 windows shown instead of 5, and I cannot figure out why.
Observations
If I remove the self._ref = self line and instead store the widgets in a persistent list object I get 5 windows
If I don't create the outer DataFrame I get 5 windows
If I inherit a QWidget instead of a QMainWindow I get 5 windows
If I add any line inside __init__ that creates a Widget I get 5 windows, such as x = QtWidgets.QPushButton()
I cannot reproduce this consistently with different versions of PyQt and pandas other than those below
PyQt==5.13.0
pandas==0.24.2
I reproduced this on two different computers. I have a feeling this is a bug with one of the libraries but would like help understanding the actual root cause here since my example requires such a specific scenario... it would not be useful as a GitHub issue and I don't even know which library is responsible.
It is actually unexpected that the windows don't close. The real bug is in your own code. You are implicitly creating reference-cycles which are randomly keeping objects alive. Python's garbage-collector does not guarantee when or in what order objects will be deleted, so the unpredictable behaviour you see is "normal". If you add import gc; gc.collect() after the for-loop, the windows will probably all be deleted. So the correct solution is to keep explict references to the windows, as you already mentioned in your first bullet-point.

PySide crash when displaying pixmaps

I am programming a GUI application for Data visualization using Python and Qt via PySide.
I experience occasional crashes ('python.exe has stopped working') which I think I narrowed down to the following problem:
When creating a pixmap from a numpy array, somehow the memory is freed by python (?) even when the pixmap already exists. This does not happen if the image format used is QImage.Format_ARGB32. (Why not?). Check out the code example below, I hope you can reproduce the problem.
EDIT: To clarify - If the numpy array is not deleted by python, everything works just as expected. However, in my application, new data is generated constantly and I would have to find a good way to track which dataset is currently displayed as a pixmap, and delete it as soon as it is not displayed anymore. I would like to find the correct way for Qt to take care of the (image-) data and store it in memory until not required anymore.
As far as I understood the documentation of Qt and PySide, the pixmap should hold all the data of the image, thus Qt should be responsible for the memory management.
Is this a bug in Qt, Pyside, or did I not understand something? I could not find any details on the memory management in the regular documentation.
Background: I need to regularly update the data to display, thus it may happen that between creating the pixmap and displaying it, the numpy data array is already overwritten by python (as there are some CPU intensive threads involved that sometimes slow the GUI). Thus, storing the numpy array forever is not an option.
Here is a code example, the interesting bits happen in the display_image method:
import numpy as np
from PySide import QtCore, QtGui
import sys
class displaywidget(QtGui.QWidget):
def __init__(self,parent = None):
super(displaywidget, self).__init__(parent)
## set up the GUI elements
self.setLayout(QtGui.QGridLayout())
self.view = QtGui.QGraphicsView()
self.layout().addWidget(self.view)
self.scene = QtGui.QGraphicsScene()
self.view.setScene(self.scene)
# create a pixmap and display it on the graphicsview
self.display_image()
def display_image(self):
# create image data in numpy array
size = 1024
r = np.linspace(0,255, num = size**2, dtype = np.uint32)
argb = (r<<16) +(255<<24)
# image should display a black to red shading
image = QtGui.QImage(argb, size,size, size*4, QtGui.QImage.Format_RGB32)
### using ARGB format option does not cause the problem
# image = QtGui.QImage(argb, size,size, size*4, QtGui.QImage.Format_RGB32)
pixmap = QtGui.QPixmap.fromImage(image)
self.scene.addPixmap(pixmap)
### when the image data is stored, everything works fine, too
# self.cache = argb
### if only the pixmap and image is stored, the problem still exists
# self.cache = [pixmap, image]
def main(argv):
## create application and main window
try:
app = QtGui.QApplication(argv)
new_qtapp = True
except:
new_qtapp = False
mainwindow = QtGui.QMainWindow()
mainwindow.setCentralWidget(displaywidget())
mainwindow.show()
if new_qtapp:
sys.exit(app.exec_())
return mainwindow
if __name__=="__main__":
w = main(sys.argv)
I am using 32 bit Python 2.7.6 and PySide 1.2.2 on a generic Windows7 Office PC.
Thanks for your help!
This simple change keeps the image from being garbage collected when the function is done. Which seems to be what caused the problem
self.argb = (r<<16) +(255<<24)
# image should display a black to red shading
image = QtGui.QImage(self.argb, size,size, size*4, QtGui.QImage.Format_RGB32)

PySide program closes instantly

I'm trying to learn PySide for a project I'm working on. I'm working through the Zetcode tutorials, but from the very beginning I'm running problems. I am writing and running my code through Enthought's Canopy. When I run the code from the command line it works fine. This question may be related to my issue, however no answer is given there.
When I use the simplest code from the tutorial
import sys
from PySide import QtGui
wid = QtGui.QWidget()
wid.resize(250, 150)
wid.setWindowTitle('Simple')
wid.show()
everything runs correctly. The next example does more or less the same, except from an OOP perspective.
import sys
from PySide import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Icon')
self.setWindowIcon(QtGui.QIcon('web.png'))
self.show()
def main():
ex = Example()
if __name__ == '__main__':
main()
When run the program flashes (I can see the window appear for a brief second) and then closes. Raising an exception before the end of main() will keep the window on the screen.
TL;DR
Why does putting the program in a class make it not work?
The difference between the two examples, is that first one keeps a reference to the widget as a global variable, whilst the second creates a local variable that gets garbage-collected when it goes out of scope (i.e. when the function returns).
The simplest way to fix this, is to make the ex variable global, like this:
def main():
global ex
ex = Example()
or you could just get rid of the main function, and simply do:
if __name__ == '__main__':
ex = Example()
The difference between running in and out of Canopy is that Canopy uses IPython QTConsole running, by default, in pylab mode with a QT GUI backend. The nice thing about this (and one of the many geniuses of ipython) is that you can have a live interaction between the command prompt and your GUI).
I suspect that you are bumping into pylab. A sophisticated program like Matplotlib can figure out whether the GUI event loop was already started, and adapt accordingly. But for your purpose, you probably just want to disable pylab mode, so that the IPython in Canopy acts more like a generic python. To do this, disable Pylab mode from the Canopy Preference menu (Python tab).

Categories

Resources