使用matplotlib_backend_qtquick在pyqt界面中集成matplotlib。

huangapple go评论82阅读模式
英文:

Integrating matplotlib in pyqt interface using matplotlib_backend_qtquick

问题

我需要在我的pyqt界面窗口中显示一些matplotlib.pyplot的3D图形。试图使用类似于这个的实现,借助库matplotlib_backend_qtquick的帮助。

以下是我的main.py代码部分:

import os
import sys

from PyQt5 import QtQml, QtCore
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
from matplotlib_backend_qtquick.backend_qtquickagg import FigureCanvasQtQuickAgg

os.environ['QT_QUICK_CONTROLS_STYLE'] = "Material"

QtQml.qmlRegisterType(FigureCanvasQtQuickAgg, "Backend", 1, 0, "FigureCanvas")

app = QApplication(sys.argv)

engine = QQmlApplicationEngine()

engine.load(QUrl.fromLocalFile('/home/vovi/PycharmProjects/pyqt5-qtquick2-example-master/pyqt5-qtquick2-example-master/pyqt5_qtquick2_example/qml/main.qml'))

if not engine.rootObjects():
    sys.exit(-1)

sys.exit(app.exec_())

当我尝试运行我的main.py时,我收到了以下错误回溯信息:

Traceback (most recent call last):
  File "/home/vovi/PycharmProjects/pyqt5-qtquick2-example-master/pyqt5-qtquick2-example-master/main.py", line 10, in <module>
    from matplotlib_backend_qtquick.backend_qtquickagg import FigureCanvasQtQuickAgg
  File "/home/vovi/PycharmProjects/pyqt5-qtquick2-example-master/pyqt5-qtquick2-example-master/venv/lib/python3.11/site-packages/matplotlib_backend_qtquick/backend_qtquickagg.py", line 7, in <module>
    from .backend_qtquick import (
  File "/home/vovi/PycharmProjects/pyqt5-qtquick2-example-master/pyqt5-qtquick2-example-master/venv/lib/python3.11/site-packages/matplotlib_backend_qtquick/backend_qtquick.py", line 11, in <module>
    from matplotlib.backends.backend_qt5 import (
ImportError: cannot import name 'MODIFIER_KEYS' from 'matplotlib.backends.backend_qt5' (/home/vovi/PycharmProjects/pyqt5-qtquick2-example-master/pyqt5-qtquick2-example-master/venv/lib/python3.11/site-packages/matplotlib/backends/backend_qt5.py)

Process finished with exit code 1

我做错了什么?

英文:

I need to show some matplotlib.pyplot 3d graphics in my pyqt interface window. Trying to implement something like this with the help of library matplotlib_backend_qtquick.

Here is my main.py:

import os
import sys

from PyQt5 import QtQml, QtCore
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
from matplotlib_backend_qtquick.backend_qtquickagg import FigureCanvasQtQuickAgg

os.environ[&#39;QT_QUICK_CONTROLS_STYLE&#39;] = &quot;Material&quot;

QtQml.qmlRegisterType(FigureCanvasQtQuickAgg, &quot;Backend&quot;, 1, 0, &quot;FigureCanvas&quot;)

app = QApplication(sys.argv)

engine = QQmlApplicationEngine()

engine.load(QUrl.fromLocalFile(&#39;/home/vovi/PycharmProjects/pyqt5-qtquick2-example-master/pyqt5-qtquick2-example-master/pyqt5_qtquick2_example/qml/main.qml&#39;))

if not engine.rootObjects():
    sys.exit(-1)

sys.exit(app.exec_())

When I'm trying to run my main.py I got a traceback:

Traceback (most recent call last):
  File &quot;/home/vovi/PycharmProjects/pyqt5-qtquick2-example-master/pyqt5-qtquick2-example-master/main.py&quot;, line 10, in &lt;module&gt;
    from matplotlib_backend_qtquick.backend_qtquickagg import FigureCanvasQtQuickAgg
  File &quot;/home/vovi/PycharmProjects/pyqt5-qtquick2-example-master/pyqt5-qtquick2-example-master/venv/lib/python3.11/site-packages/matplotlib_backend_qtquick/backend_qtquickagg.py&quot;, line 7, in &lt;module&gt;
    from .backend_qtquick import (
  File &quot;/home/vovi/PycharmProjects/pyqt5-qtquick2-example-master/pyqt5-qtquick2-example-master/venv/lib/python3.11/site-packages/matplotlib_backend_qtquick/backend_qtquick.py&quot;, line 11, in &lt;module&gt;
    from matplotlib.backends.backend_qt5 import (
ImportError: cannot import name &#39;MODIFIER_KEYS&#39; from &#39;matplotlib.backends.backend_qt5&#39; (/home/vovi/PycharmProjects/pyqt5-qtquick2-example-master/pyqt5-qtquick2-example-master/venv/lib/python3.11/site-packages/matplotlib/backends/backend_qt5.py)

Process finished with exit code 1

What am I doing wrong?

答案1

得分: 1

matplotlib_backend_qtquick库不再维护。它现在已经损坏,因为至少它使用了旧版Matplotlib的内部符号 - MODIFIER_KEYSREADME中写着它适用于Matplotlib 3.2.1版本。

所以要么使用Matplotlib 3.2.1版本,要么自己移植这个库。

英文:

That matplotlib_backend_qtquick library is not maintained anymore. It's broken now because at least it was using an internal symbol of an old Matplotlib version - MODIFIER_KEYS. The README says it worked with Matplotlib 3.2.1.

So either use Matplotlib 3.2.1 version or port the library yourself.

huangapple
  • 本文由 发表于 2023年6月18日 21:21:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定