简体   繁体   中英

Toggling button state on matplotlib NavigationToolbar programmatically

I am embedding a figure with a customized NavigationToolbar2WxAgg so that I can (hopefully) easily control the state of the toolbar. I would like to be able to toggle the zoom and pan buttons, but I'm having some difficulty getting it to work properly. My code for the toolbar is

class CustomNavToolbar(NavigationToolbar2WxAgg):
    def __init__(self, *args, **kwargs):
        NavigationToolbar2WxAgg.__init__(self, *args, **kwargs)

        self.DeleteToolByPos(7)  # Deletes the adjust subplots button

    @property
    def IsActive(self):
        return self._active is not None

    def DeactivateAll(self):
        if self._active == 'PAN':
            self.pan('off')
        elif self._active == 'ZOOM':
            self.zoom()

with the idea that I can just call DeactivateAll() method to turn off any mode that is currently active. Unfortunately, this doesn't work quite right. The mode is disabled, but the button itself stays toggled. And when I click the button again, the mode is enabled, but the button is untoggled.

My google-fu couldn't come up with a working solution, can someone point me towards the right method?

I would do this by programmatically pushing the button.

If you look at the code for zoom there is the line

self.ToggleTool(self.wx_ids['Pan'], False)

which I assume toggles the button off, so just include a similar line:

def DeactivateAll(self):
    if self._active == 'PAN':
        self.pan('off')
        self.ToggleTool(self.wx_ids['Pan'], False)
    elif self._active == 'ZOOM':
        self.ToggleTool(self.wx_ids['Zoom'], False)
        self.zoom()

I suspect you can make this even simpler:

 def DeactivateAll(self):
    self.ToggleTool(self.wx_ids['Pan'], False)
    self.ToggleTool(self.wx_ids['Zoom'], False)

and let the call-backs do their jobs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM