0

我想将标尺添加到使用 tkinter 和 matplotlib 创建的 gui。已经有一个自定义工具栏,但它目前只使用已经提供的工具:

class CustomToolbar(NavigationToolbar2TkAgg):
"""
A custom Matplotlib toolbar that allows for a lasso selection when no tool is selected
"""
def __init__(self, canvas_, parent_, edit_frame, app):
    self.parent_frame = parent_
    self.edit_frame = edit_frame
    self.toolitems = (
        ('Home', "Reset zoom", 'home', 'home'),
        ('Back', 'Undo one zoom step', 'back', 'back'),
        ('Forward', 'Redo one zoom step', 'forward', 'forward'),
        (None, None, None, None),
        (None, None, None, None),
        (None, None, None, None),
        ('Pan', 'Activate pan', 'move', 'pan'),
        ('Zoom', 'Activate zoom', 'zoom_to_rect', 'zoom'),
        # ("Lasso", "Activate lasso", "hand", "lasso")
    )
    self.app = app
    NavigationToolbar2TkAgg.__init__(self, canvas_, parent_)

def pan(self):
    NavigationToolbar2TkAgg.pan(self)
    if self._active:
        self.edit_frame.config(background='white', text='Pan')
    else:
        self.edit_frame.config(background='red', text='Draw')
        self.app.change_class()

def zoom(self):
    NavigationToolbar2TkAgg.zoom(self)
    if self._active:
        self.edit_frame.config(background='white', text='Zoom')
    else:
        self.edit_frame.config(background='red', text='Draw')
        self.app.change_class()

如果“未选择”平移或缩放,则使用充当套索的绘图工具。套索用于选择您要标记的区域,然后在右侧图像上显示为标记。这是 GUI 的图像,其中“双边界”作为当前选择的标签(未选择时为深粉红色)。

GUI 工具的图像

所以,有人在 github 上创建了一个标尺工具:https ://github.com/terranjp/matplotlib-tools

我发现了几篇密切相关的文章:创建图标:添加工具栏按钮图标 matplotlib 自定义工具栏的不同方法:http ://dalelane.co.uk/blog/?p=778

我不确定如何在 GUI 环境中实现按钮/工具。这里的任何方向将不胜感激。

4

0 回答 0