pyqt5 closeEvent issue with matplotlib FigureCanvas - python

I wants to build some GUI for retrieving and displaying some data in real time. So I though of embedding a matplotlib figure in a pyqt5 GUI, and followed this example here (https://matplotlib.org/examples/user_interfaces/embedding_in_qt5.html). However, there will be some resource for my application that require a safe destruction at exit. So I tried to overwrite the closeEvent() function of MyMplCanvas with
def closeEvent(self,ce):
print('closeEvent')
super().closeEvent(ce)
But it does not seem to show up when I run the program. Is this expected or is there anything wrong with the program? If this is not the proper way to do so, what is the right way to do some clean-up during the destruction of a QWidget?
Thanks a lot!

Related

PyQt5: Get GL parameters from QtOpenGLWidget without showing first

Is it possible to use GL functions like glGetString without showing the widget first?
The basic use case would be taking this helloworld and instead of showing the widget, maybe I want to print a command line usage message with the GPU information and GL version. The script currently does this already after showing the widget.
In my own application I have a subclass of a QOpenGLWidget and I've tried manually creating a QOpenGLContext, setting the format, and making it current (with and without .create()), but that didn't seem to work. The subclass seems to work fine when shown.
Any help is much appreciated.
Further details: I'm one of the maintainers of the python library vispy and I'm trying to update our PyQt5 backend to use QOpenGLWidget instead of QGLWidget. The pull request is here if anyone wants to tell us what we're doing wrong: https://github.com/vispy/vispy/pull/1394
A OpenGL context needs a "window" (exactly needs a "Device Contex" or a "Display", depending on the OS) to render to. That window doesn't need to be visible, just that the gl context is current to it.
The easiest way is to hide() (or don't call show()) the window at its creation. You can make it visible later.

Background process and tkinter

Looking for help on where to start with this, not too good with Python. What I trying to do is use tkinter for a gui interface but i need to be able to process recieved data and update labels widgets as information changes. I all ready have the communication portion of my program working fine in the shell but when I try to tie it to tkinter it will stop processing as soon as the interface is generated. Anyone have a simple code for me to modify to my needs or point me to a reference example somewhere. Spent days so far trying different options and I still have yet to find something that works.
Thanks for any help
Convert your working program into functions that you can register as callbacks in the tkinter UI (say buttons, or other widgets), that is, make it event-driven, and then, for background processing register some of the functions with the after widget method. The root.mainloop() will never return (only on UI close), use it as the last instruction.
So you can't just write your logic in a top-down structure, and hope that it will work well with the UI. The mainloop will be permanently looping, and will call specific funtions in your code, as appropriate to the received events from the user, or to callbacks you registered to run after some time with after.
See here for the after part
Take a look here for structuring tkinter programs. It should have enough info and links for you to study and learn how to do it in a right way.

How to remove icon from QMessageBox in PyQt5?

I am trying to code a message box that just pops up and says something and has a single button to close it however as I only have a small amount of text in the box the icon/image to the left of it is huge and looks bad. I want to know how to remove it. I am also having trouble making custom message boxes. Tutorials say that this is how you make a custom message box:
box = QMessageBox()
box.setText('text')
box.addButton(QPushButton('Close', self))
box.exec_()
However this just closes my program and returns a 1. My current code uses the about method of QMessageBox():
box = QMessageBox().about(self, 'About', 'This is a test Program')
However this has that large icon in the text window and I can't seem to do anything else to the box as it just stops the program and returns 1 again
I am in desperate need of some decent PyQt documentation. I can't seem to find documentation on much at all unless it is in C++. For instance I cannot seem to find any information of options other than question and about for QmessageBox. So if someone could also show me where some proper documentation lives it would prevent me asking too many questions here
Rather than PyQt documentation, it is better to directly use Qt documentation. You only need to switch your language mindset from Python to C++, there and back. It is not that difficult. :) See e.g. http://doc.qt.io/qt-4.8/qmessagebox.html#addButton or http://doc.qt.io/qt-4.8/qmessagebox.html#about I think this is very detailed documentation, unrivaled by most other frameworks.
Note that there are three overrides of addButton(). From the documentation it seems that you either need to pass two arguments to box.addButton(QPushButton('Close', self), QMessageBox.RejectRole) (you forgot the role!) or better, you use the override which uses standard buttons, then you only pass one argument: box.addButton(QMessageBox.Close).
And one more tip for you: I also find it easier to debug my program with PySide than PyQt because unlike PyQt, PySide catches the exception, prints that to console and keeps running. While PyQt usually just silently crashes leaving you clueless. Most of the time, I am using shims Qt.py https://pypi.python.org/pypi/Qt.py/0.6.9 or qtpy https://pypi.python.org/pypi/QtPy to be able to switch from PyQt to PySide on the fly. It also allows switching between Qt4 and Qt5 bindings easily.

Best Qt Widget to use for properties window... in PySide?

This question is the exact same as this one except that I'm looking for such a widget to be used in PySide. Anyone knows some code out there that provide easy-to-reuse property editor widget?
There is the proposal of using QTreeView or QTableView to build such a widget that is an option with PySide, but this is not a straight forward solution...
I don't know if you would be OK with this, but if you are happy to add the whole of pyqtgraph as a dependency, then you might want to try using pyqtgraph's ParameterTree.
There is a pretty comprehensive set of examples, just install pyqtgraph and then run:
import pyqtgraph.examples
pyqtgraph.examples.run()
Launch the ParameterTree example!

Why is my wx.App interfering with my matplotlib plots?

I am trying to build a GUI with wxPython to control a pretty stable, well-established model. The standard output of the model includes a series of plots to screen done via matplotlib. These plots are all OK if the model is run from the command line. If I run from my new GUI, however, they all show up, but I have no control over them: I can't click their respective "save" buttons; if they get covered up by other windows, they are "grayed out" when the blocking window is moved away. Things like that. If I close my GUI window, however, I get full control of my plots again. But this is no way to run this code.
I think my procedure is pretty standard. The bare outlines of the code are:
import wx
import model_code
class gui_for_model(wx.Frame):
# lots of stuff with hooks into model_code,
# including a "go" button that starts model_code running
#end of class
app=wx.App(False)
gm = gui_for_model()
app.MainLoop()
Anyone know what I should do differently? Thanks.
Here is a link to a site that has some demo code of using matplotlib with wxPython GUIs.
eli.thegreenplace.net/2008/08/01/matplotlib-with-wxpython-guis
Also this - matplotlib.org/faq/usage_faq.html talks about changing the backend.
And i found a package on pypi pypi.python.org/pypi/wxmplot/0.9.12

Categories

Resources