Python Add-In Extension enabled state - how to use?

865
3
Jump to solution
08-01-2014 09:30 AM
theMC5
by
New Contributor

I'm trying to sort out the proper use of the extension.enabled property.

Extensions, Python or otherwise, usually have a toolbar. But the toolbar can be displayed whether or not the extension is checked (enabled) in the Extensions... dialog box. So then I'd at least want my Add-In's toolbar to be disabled if the extension is disabled, and to enable if the extension is enabled.

It is easy enough, if messy, for every widget on the toolbar to check in its init() whether it should enabled itself when it is created (when the toolbar is shown) - but what about after the toolbar has been shown, if the extension enabled state is changed? In a Python Add-In extension at least, there is no event on the extension's enabled property. Where should I hang code to reset the toolbar widgets' enabled state?

In the end, I want some mechanism so the user can disable everything on my Add-In's toolbar when they don't want to use it. Is the extension enabled property the right place for that sort of thing?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Occasional Contributor III

You could have a special attribute like _enabled, then only iff self._enabled AND the extension is enabled is the button enabled:

class MyButtonClass(object):

    _enabled = True # Enabled by default

    def onClick(self):

        # ...do stuff...

        pass

    @property

    def enabled(self):

        try:

            return self._enabled and my_extension.enabled

        except NameError:

            return False

Then you just set self._enabled as you normally would set self.enabled and you get a composite behavior where the button is enabled only when it wants to be and the extension is ready to go too.

View solution in original post

3 Replies
JasonScheirer
Occasional Contributor III

If your extension's ID is MyAddin_addin.my_extension and you've got a button whose ID is MyAddin_addin.my_button based on the class MyAddin_addin.MyButtonClass, you can define .enabled as a property on your button class:

class MyButtonClass(object):

    def onClick(self):

        # ...do stuff...

        pass

    @property

    def enabled(self):

        try:

            return my_extension.enabled

        except NameError:

            return False

Now every time the app checks if your button is enabled, the button will pass through and query the extension.

0 Kudos
theMC5
by
New Contributor

thanks, that is sneaky and working well. how would you extend that override if there are circumstances in my code where I want to set the buttons enabled state (like based on state of other controls?

I suppose it will start something like this, but I don't know what to set....

  @enabled.setter

  def enabled(self, newval):

   <some private enabled field>?? = newval

0 Kudos
JasonScheirer
Occasional Contributor III

You could have a special attribute like _enabled, then only iff self._enabled AND the extension is enabled is the button enabled:

class MyButtonClass(object):

    _enabled = True # Enabled by default

    def onClick(self):

        # ...do stuff...

        pass

    @property

    def enabled(self):

        try:

            return self._enabled and my_extension.enabled

        except NameError:

            return False

Then you just set self._enabled as you normally would set self.enabled and you get a composite behavior where the button is enabled only when it wants to be and the extension is ready to go too.