Python 如何更改matplotlib工具栏的平移和缩放按钮的模式

Python 如何更改matplotlib工具栏的平移和缩放按钮的模式,python,matplotlib,toolbar,Python,Matplotlib,Toolbar,我在tkinter应用程序中嵌入了一个matplotlib图形,并在其中附加了一个自定义工具栏。 以下是工具栏的代码: class CustomToolbar(NavigationToolbar2Tk): def __init__(self, canvas_, parent_, background_color): self.toolitems = ( ('Home', 'Réinitialiser la vue', 'home', 'home

我在tkinter应用程序中嵌入了一个matplotlib图形,并在其中附加了一个自定义工具栏。 以下是工具栏的代码:

class CustomToolbar(NavigationToolbar2Tk):

    def __init__(self, canvas_, parent_, background_color):

        self.toolitems = (
            ('Home', 'Réinitialiser la vue', 'home', 'home'),
            ('Back', 'Retour à la vue précédente', 'back', 'back'),
            ('Forward', 'Avancer à la vue suivante', 'forward', 'forward'),
            (None, None, None, None),
            ('Pan', 'Clic gauche : Déplacer le graphe\nClic droit: Dilater/compresser les axes', 'move', 'pan'),
            ('Zoom', 'Zoomer', 'zoom_to_rect', 'zoom'),
            (None, None, None, None)
        )

        super().__init__(canvas_, parent_)

        self.background_color = background_color
        self.configure(bg=self.background_color)
        self.get_message_label().config(background=self.background_color)
        self.update()

    def get_message_label(self):
        return self._message_label

    def pan(self):
        super().pan(self)
        self.mode = "Déplacer/Dilater"
        self.set_message(self.mode)

    def zoom(self):
        super().zoom(self)
        self.mode = "Zoomer"
        self.set_message(self.mode)
我重新定义了
平移
缩放
方法,以便在平移或缩放按钮处于活动状态时设置新模式,之所以这样做是因为我想更改
\u message\u label
中显示的文本。我这样做了,正如对这个问题的回答所表明的那样:

但是,该解决方案在2020年似乎不起作用,因为模式在matplotlib.backend_库中的
\u Mode
类中预定义为类成员:

class _Mode(str, Enum):
    NONE = ""
    PAN = "pan/zoom"
    ZOOM = "zoom rect"

    def __str__(self):
        return self.value

    @property
    def _navigate_mode(self):
        return self.name if self is not _Mode.NONE else None
我试图在脚本中更改类成员的平移和缩放,但
枚举
类的
\uuuu setattr\uuuuuuuuuuuuuu>方法不允许我:

    def __setattr__(cls, name, value):
        """Block attempts to reassign Enum members.

        A simple assignment to the class namespace only changes one of the
        several possible ways to get an Enum member from the Enum class,
        resulting in an inconsistent Enumeration.

        """
        member_map = cls.__dict__.get('_member_map_', {})
        if name in member_map:
            raise AttributeError('Cannot reassign members.')
        super().__setattr__(name, value)
那么,是否可以简单地更改类成员的平移和缩放而不影响matplotlib.backend_base.py文件

谢谢你抽出时间


Paul

好的,所以我找到了一个解决方案,当包装
NavigationToolbar2Tk
类的
set\u消息时,它似乎起到了作用,如下所示:

    def set_message(self, s):
        if s == "pan/zoom":
            self.message.set("Déplacer/Dilater")

        elif s == "zoom rect":
            self.message.set("Zoomer")
我在问题中提到的帖子中指出了这个解决方案,但我想我根本没有看到它