Matplotlib animation inside your own GUI

I think I found the solution. All credit goes to Mr. Harrison who made the Python tutorial website https://pythonprogramming.net. He helped me out. So here is what I did. Two major changes: 1. Structural change I previously had two classes: CustomGraph(TimedAnimation) and CustomFigCanvas(FigureCanvas). Now I got only one left, but he inherits from both TimedAnimation … Read more

PyQt Connect to KeyPressEvent

Create a custom signal, and emit it from your reimplemented event handler: class MyWidget(QtGui.QWidget): keyPressed = QtCore.pyqtSignal(int) def keyPressEvent(self, event): super(MyWidget, self).keyPressEvent(event) self.keyPressed.emit(event.key()) … def on_key(key): # test for a specific key if key == QtCore.Qt.Key_Return: print(‘return key pressed’) else: print(‘key pressed: %i’ % key) self.widget.keyPressed.connect(on_key) (NB: calling the base-class implementation is required in order … Read more

PyQt: RuntimeError: wrapped C/C++ object has been deleted

This answer to this question is as found here: Python PySide (Internal c++ Object Already Deleted) Apparently, assigning one widget to QMainWindow using setCentralWidget and then assigning another widget with setCentralWidget will cause the underlying c++ QWidget to be deleted, even though I have an object that maintains reference to it. Note: QMainWindow takes ownership … Read more

How can I check if a keyboard modifier is pressed (Shift, Ctrl, or Alt)?

It looks like all you need to do is check the keyboardModifiers in your button handler, and select a different action as appropriate. The various modifiers can be OR’d together in order to check for multi-key combinations: PyQt5: import sys from PyQt5 import QtCore, QtWidgets class Window(QtWidgets.QWidget): def __init__(self): super().__init__() self.button = QtWidgets.QPushButton(‘Test’) self.button.clicked.connect(self.handleButton) layout … Read more

PyQt – how to detect and close UI if it’s already running?

Here is a very simple PyQt5 solution using QLockFile: from PyQt5 import QtCore, QtWidgets lockfile = QtCore.QLockFile(QtCore.QDir.tempPath() + ‘/my_app_name.lock’) if lockfile.tryLock(100): app = QtWidgets.QApplication([]) win = QtWidgets.QWidget() win.setGeometry(50, 50, 100, 100) win.show() app.exec() else: print(‘app is already running’) There were a couple of fairly straightforward C++ solutions given on the Qt Wiki which no longer … Read more

Clear all widgets in a layout in pyqt

After a lot of research (and this one took quite time, so I add it here for future reference), this is the way I found to really clear and delete the widgets in a layout: for i in reversed(range(layout.count())): layout.itemAt(i).widget().setParent(None) What the documentation says about the QWidget is that: The new widget is deleted when … Read more

PyQt4 to PyQt5 -> mainFrame() deprecated, need fix to load web pages

you must call the QWebEnginePage::toHtml() inside the definition of the class. QWebEnginePage::toHtml() takes a pointer function or a lambda as a parameter, and this pointer function must in turn take a parameter of ‘str’ type (this is the parameter that contains the page’s html). Here is sample code below. import bs4 as bs import sys … Read more