I want to customise the icons (like home, back, forward, pan etc) on the matplotlib toolbar. In this case I am writing a GUI with tkinter with embedded pyplot plot in the widget of the GUI and create the toolbar with NavigationToolbar2Tk
.
After searching for a bit I didn't find a clear method to do it. My first approach was to define a child class that inherits everything from NavigationToolbar2Tk
and than modify the method where the paths to the icons are located, placing the paths to my custom icons. I was trying to do something like this:
class custom_NavigationToolbar2Tk(NavigationToolbar2Tk):
"""Make a custom toolbar with custom icons"""
def _set_image_for_button(self, button):
"""
Ovveride this method to chose custom icons
"""
path_regular = pathlib.Path(os.path.join(os.path.dirname(__file__), "icons", os.path.basename(button._image_file)))
_set_image_for_button
is the name of the method where the variable containing the icons paths path_regular
is located. I want to only overide this variable from the parent class, while keeping everything else the same. When in my main code I call custom_NavigationToolbar2Tk
it does create the toolbar but it seems to completely ignore the _set_image_for_button
method in my code, so the icons remain the old one.
I found a workaround that works, that is I define only the _set_image_for_button
method in my code with inside the entire code from NavigationToolbar2Tk._set_image_for_button
, only replacing the original expression for the path_regular
variable with my expression that gives the location of my custom icons and than I overide the original method like this:
NavigationToolbar2Tk._set_image_for_button = _set_image_for_button
And this does work as expected with old icons replaced by my new icons. But I dont like very much this solution and don't understand why the first approach doesn't work.
I found only 1 analogous question here How do I configure a MatPlotLib Toolbar so that the buttons in the toolbars are ttk buttons instead of old Tk buttons but the solution doesn't work.