Python 如何将自定义切换工具添加到matplotlib工具栏inPyQt5应用程序?

Python 如何将自定义切换工具添加到matplotlib工具栏inPyQt5应用程序?,python,matplotlib,user-interface,pyqt5,toolbar,Python,Matplotlib,User Interface,Pyqt5,Toolbar,我正在制作一个带有嵌入式matplotlib画布的PyQt5应用程序。我需要将自定义工具添加到Matplotlib工具栏,该工具栏的切换方式与Matplotlib的默认工具“缩放”和“平移”相同。问题是Matplotlib的NavigationToolbar2QT类似乎并没有直接添加工具的函数,而ToolbarQt我无法使用 以下是使用NavigationToolbar2QT的我的试用代码的简化版本: import sys from PyQt5 import QtWidgets, uic fr

我正在制作一个带有嵌入式matplotlib画布的PyQt5应用程序。我需要将自定义工具添加到Matplotlib工具栏,该工具栏的切换方式与Matplotlib的默认工具“缩放”和“平移”相同。问题是Matplotlib的NavigationToolbar2QT类似乎并没有直接添加工具的函数,而ToolbarQt我无法使用

以下是使用NavigationToolbar2QT的我的试用代码的简化版本:

import sys

from PyQt5 import QtWidgets, uic
from matplotlib.backends.backend_qt5agg import (
    FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure

ui_file = 'user_interface.ui'


class ToolBar(NavigationToolbar):
    def __init__(self, figure_canvas, parent=None):
        NavigationToolbar.__init__(self, figure_canvas, parent=None)
        # Add custom tools here


class MplWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        self.canvas = FigureCanvas(Figure())
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.canvas)
        self.setLayout(layout)
        self.canvas.figure.add_subplot(111)
        self.canvas.draw()


class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi(ui_file, self)
        self.toolbar = ToolBar(self.MplWidget.canvas)
        self.ToolBarLayout.addWidget(self.toolbar)
        self.show()


app = QtWidgets.QApplication(sys.argv)
window = Ui()
sys.exit(app.exec_())
我知道我可以通过将新工具添加到NavigationToolbar2QT的toolitems中,将其添加到自定义工具栏类中,但这样做无法实现切换功能

我还尝试使用Toolbar Qt执行此操作:

import sys

from PyQt5 import QtWidgets, uic
from matplotlib.backends.backend_qt5 import ToolbarQt
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.backend_managers import ToolManager
from matplotlib import backend_tools
from matplotlib.figure import Figure
from matplotlib import pyplot
import matplotlib
pyplot.rcParams['toolbar'] = 'toolmanager'
matplotlib.use('Qt5Agg')

ui_file = 'user_interface.ui'


class MplWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        self.canvas = FigureCanvas(Figure())
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.canvas)
        self.setLayout(layout)
        self.canvas.figure.add_subplot(111)
        self.canvas.draw()


class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi(ui_file, self)
        self.tool_manager = ToolManager(self.MplWidget.canvas.figure)
        backend_tools.add_tools_to_manager(self.tool_manager)
        self.toolbar = ToolbarQt(self.tool_manager, self)
        backend_tools.add_tools_to_container(self.toolbar)
        self.ToolBarLayout.addWidget(self.toolbar)
        # Add custom tool here
        self.show()


app = QtWidgets.QApplication(sys.argv)
window = Ui()
sys.exit(app.exec_())
但是,在这种情况下,即使使用了Qt5后端,即使是默认工具也会导致以下错误

AttributeError: 'FigureCanvasQTAgg' object has no attribute '_tkcanvas'

你有什么进展吗?你有什么进展吗?