Select to view content in your preferred language

onUpdate() for python add ins?

1696
1
11-13-2014 02:01 PM
CarlWalter
Deactivated User

When developing .Net add ins I would often use onUpdate() to determine when a button was enabled.  In the help file on Managing the State of Python Add-in types it says the following:

Add-in types--buttons, tools, and combo boxes--are periodically notified to update their enabled and checked state. In Python, add-in types contain Boolean properties for enabled and checked. For example, you can set the add-in to be enabled or disabled and checked or unchecked during its initialization.

When and how is this 'periodic notification' happening and how can I take advantage of this?

I have a tool that should only be enabled if the current layer has a selection.  Something like this to check if the current layer has a selection and if it does, enable the tool.

layer = pythonaddins.GetSelectedTOCLayerOrDataFrame()

if type(layer) is arcpy._mapping.Layer and not layer.isGroupLayer and arcpy.Describe(layer).fidSet

    self.enabled = True

else:

    self.enabled = False

I see that extensions allow some control over whether or not a tool is enabled, but I don't see anything that would be enabled if something simple like a selection on a layer was made.  This would be simple in .NET.  I also realize I could have a message box that pops up if there is no selection and says "Hey User!  Make a selection on this layer and try this button again" but in my mind the button should only be enabled if a selection exists on the current layer and should update whenever these types of changes occur.

Also it would be great if I could control the location of where the button is placed.  For instance I'd love for this button to just appear in the feature layer>selection context menu, instead of telling users to put it there.  Again, easy in .NET, but doesn't seem to be possible in a python add-in.  This one, I'm not as concerned about for right now though...

Tags (2)
0 Kudos
1 Reply
BryanTaylor
Emerging Contributor

Carl, did you ever find an answer or useful solution for this?

0 Kudos