Select to view content in your preferred language

Python Addin Action based on Property Value

908
6
Jump to solution
09-12-2012 08:20 AM
JohnDye
Deactivated User
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?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JohnDye
Deactivated User
SOLVED!!!

Thanks for recommendations! Here's the solution for turning layers and off with a single button button:

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")         for lyr in arcpy.mapping.ListLayers (mxd):             if lyr.name == "COF_Cust":                 if lyr.visible == False:                     lyr.visible = True                     arcpy.RefreshActiveView()                 elif lyr.visible == True:                     lyr.visible = False                     arcpy.RefreshActiveView()                 else:                     pass 


It's important to note that the self.checked property does not loop when layers are turned on or off, like I thought it would. It seems to remain constant initialization. I thought when I turned a layer on, it would change automatically to True but it doesn't. I tested this by inserting a print statement and converting the boolean value that the property held to string and printing the value as I turned the layer on and off. It remained set to false regardless of layer visibility. I suppose I miunderstood what the self.checked property is doing.

View solution in original post

0 Kudos
6 Replies
MikeMacRae
Frequent Contributor
Hey, your equals sign should be a double egual sign in an if/else statement like this:

if self.checked == True


Also, you might want to loop through your layers, instead of indexing. I always seem to have problems with the indexing,s o I usually do something like this:

for lyr in arcpy.mapping.ListLayers(mxd):

    if lyr.name == "your layer name":
        if self.checked == True:
            "your layer name".visible = True
            arcpy.RefreshActiveView()
            print "Customers On!"
        else:
            "your layer name".visible = False
            arcpy.RefreshActiveView()
        print "Customers Off!"


Hope that's what you're looking for.

Mike
0 Kudos
JohnDye
Deactivated User
Thanks Mike,
That kind of worked. The only problem now is that because the button initializes with

self.checked = False


I now have to figure out how to make the property change to the opposite of the current value whenever the button is clicked. Otherwise, it will always be set to False.
0 Kudos
JasonScheirer
Esri Alum
Use the not operator.

layer.visible = not layer.visible
self.checked = layer.visible
0 Kudos
JohnDye
Deactivated User
Jason,
I'm not sure what you mean. I mean I understand what the not operator is, just as I read the code, I'm not sure I understand how you're suggesting I implement it. Would you mind posting a more throughout example?
0 Kudos
JohnDye
Deactivated User
SOLVED!!!

Thanks for recommendations! Here's the solution for turning layers and off with a single button button:

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")         for lyr in arcpy.mapping.ListLayers (mxd):             if lyr.name == "COF_Cust":                 if lyr.visible == False:                     lyr.visible = True                     arcpy.RefreshActiveView()                 elif lyr.visible == True:                     lyr.visible = False                     arcpy.RefreshActiveView()                 else:                     pass 


It's important to note that the self.checked property does not loop when layers are turned on or off, like I thought it would. It seems to remain constant initialization. I thought when I turned a layer on, it would change automatically to True but it doesn't. I tested this by inserting a print statement and converting the boolean value that the property held to string and printing the value as I turned the layer on and off. It remained set to false regardless of layer visibility. I suppose I miunderstood what the self.checked property is doing.
0 Kudos
JohnDye
Deactivated User
Just realized exactly what the Checked property is doing for anyone reading this post. It's simply allowing a button you create to apper pressed or not. See http://resources.arcgis.com/en/help/main/10.1/index.html#//014p00000029000000.
0 Kudos