0

I want to add a ruler to a gui created with tkinter and matplotlib. There is already a custom toolbar but it currently only utilizes tools already provided:

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()

if either the pan or zoom are 'unselected' the drawing tool is used which acts as a lasso. The lasso is used to select the region you would like to label and then it shows up as labeled on the right hand image. Here is an image of what the GUI looks like with "twin boundary" as the currently selected label (which is dark pink when unselected).

Image of GUI tool

So, There was a ruler tool created by someone on github: https://github.com/terranjp/matplotlib-tools

And I have found a couple closely related articles: create the icon: Add toolbar button icon matplotlib different method for custom toolbar: http://dalelane.co.uk/blog/?p=778

I am not sure how to implement the button/tool in a GUI environment. Any direction here would be greatly appreciated.

Amanda.py
  • 113
  • 10
  • Did you read [this](https://stackoverflow.com/questions/37506260/adding-an-item-in-matplotlib´s-toolbar)? – ImportanceOfBeingErnest Dec 01 '18 at 21:24
  • Yes I did, I pretty much followed the code there but it isn't working because it's looking for the associated 'ruler' gif. self.tk.call(('image', 'create', imgtype, name,) + options) _tkinter.TclError: couldn't open "C:\Users\proxi\Anaconda3\lib\site-packages\matplotlib\mpl-data\images\ruler.gif": no such file or directory – Amanda.py Dec 02 '18 at 01:13
  • I guess if you're looking for a runnable piece of code you should provide one yourself to start with. – ImportanceOfBeingErnest Dec 02 '18 at 18:11
  • True, I guess I could make a bare bones version of the code that just uses one specific image for this kind of purpose. Will get back with that. – Amanda.py Dec 02 '18 at 19:55

0 Answers0