AnsweredAssumed Answered

Python Addin Action based on Property Value

Question asked by johnmdye on Sep 12, 2012
Latest reply on Dec 20, 2012 by johnmdye
So I'm relatively new to this but I'm trying to create a simple button through a Python Addin that will turn a layer on or off. I can get the layer to turn on, but not off when I reclick the same button. I figured that if I used the self.checked property for the buttonit would work but then I got the [Missing] Error indicating my syntax was bad. Idle was referencing the = sign I used in the If/Else statement when I went to perform the action based off of the property value. Here's the code that gave me the [Missing] error:

import arcpy import pythonaddins  class Toggle_Cust(object):     """Implementation for C1_Cust.button (Button)"""     def __init__(self):         self.enabled = True         self.checked = False     def onClick(self):         #Get the current Map Document and Create the Layer Index.         mxd = arcpy.mapping.MapDocument("Current")         COF_Customers = arcpy.mapping.ListLayers (mxd) [1]         #Turn the Layer On or Off depending on property value         if self.checked = True:             COF_Customers.visible = True             arcpy.RefreshActiveView()             print "Customers On!"         else:             COF_Customers.visible = False             arcpy.RefreshActiveView()         print "Customers Off!" 


So just to test the premise I just tried turning on the layer with
import arcpy import pythonaddins  class Toggle_Cust(object):     """Implementation for C1_Cust.button (Button)"""     def __init__(self):         self.enabled = True         self.checked = False     def onClick(self):         #Get the current Map Document and Create the Layer Index.         mxd = arcpy.mapping.MapDocument("Current")         lyrlist = arcpy.mapping.ListLayers (mxd) [0]         #Identify the Customer Layer by Index Value         COF_Customers = arcpy.mapping.ListLayers (mxd) [1]         COF_Customers.visible = True         arcpy.RefreshActiveView()         print "Customers On!" 


That works to just turn on the layer. Anyone got any advice on turning the layer back off with another click of the same button?

Outcomes