How To Have Qt Run Asynchroneously For Interactive Use Like Matplotlib's Ion Mode?
I want to be able to launch a Qt interface from the python interpreter, with the command line returning immediately, so that I can continue using python while being able to use the
Solution 1:
It is not necessary to use a thread or the complement of another library, you just have to execute the commands but you should not call the exec_() method of QApplication since it uses the python interactive console eventloop.
$ python
Python 3.8.2 (default, Feb 26 2020, 22:21:03)
[GCC 9.2.1 20200130] on linux
Type "help", "copyright", "credits" or "license" for more information
>>>from PyQt5.QtWidgets import QApplication, QGraphicsRectItem, QGraphicsScene, QGraphicsView, QMainWindow>>>classRect(QGraphicsRectItem):...defmousePressEvent(self, event):...print("foo")...>>>app = QApplication([])>>>window = QMainWindow()>>>window.setGeometry(100, 100, 400, 400)>>>view = QGraphicsView()>>>scene = QGraphicsScene()>>>rect = Rect(0, 0, 150, 150)>>>scene.addItem(rect)>>>view.setScene(scene)>>>window.setCentralWidget(view)>>>window.show()
IPython
As the IPython docs points out, %gui backend
must be used to enable the GUI event loops. In the case of PyQt5/PySide2, %gui qt5
must be used at the beginning.
$ ipython
Python 3.8.2 (default, Feb 262020, 22:21:03)
Type'copyright', 'credits'or'license'for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type'?'forhelp.
In [1]: %gui qt5
In [2]: from PyQt5.QtWidgets import QApplication, QGraphicsRectItem, QGraphicsScene, QGraphicsView, QMainWindow
In [3]: classRect(QGraphicsRectItem):
...: defmousePressEvent(self, event):
...: print("foo")
...:
In [4]: app = QApplication([])
In [5]: window = QMainWindow()
In [6]: window.setGeometry(100, 100, 400, 400)
In [7]: view = QGraphicsView()
In [8]: scene = QGraphicsScene()
In [9]: rect = Rect(0, 0, 150, 150)
In [10]: scene.addItem(rect)
In [11]: view.setScene(scene)
In [12]: window.setCentralWidget(view)
In [13]: window.show()
Post a Comment for "How To Have Qt Run Asynchroneously For Interactive Use Like Matplotlib's Ion Mode?"