Layer Label Toggle

1344
9
Jump to solution
03-03-2017 12:09 PM
VishalShah2
Occasional Contributor II

I am trying to write a python script for a tool that lets you toggle the labels for specific feature classes using Boolean values. My question is how can I get the labels to turn on or off? I've seen examples of people using the variable.showLabels = True to get their labels to display but for me it did not work. Any help is greatly appreciated.

if (turnLabelsOnOff == True) and (layer in layerList):
    layer.showLabels = True

if (turnLabelsOnOff == False) and (layer in layerList):
    layer.showLabels = False

if (turnLabelsOnOff == True) and (layer in layerList):
    layer.showLabels = True

if (turnLabelsOnOff == False) and (layer in layerList):
    layer.showLabels = False
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Vishal,

Instead of referencing the variables via their quoted name, reference the layers as such:

x = arcpy.mapping.ListLayers(mxd, "X")[0]
y = arcpy.mapping.ListLayers(mxd, "Y")[0]
layerList = [x, y]‍‍‍‍‍‍‍‍‍‍

One variation of the code that you have posted before will work as well now that the variables are being referenced correctly. 

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("LABELCLASSES"):
        if lyr in layerList:
            lyr.showLabels = False‍‍‍‍

You'll want to throw in your toggle variable as well, but that is just an extra if statement line just before the showLabels action.

View solution in original post

9 Replies
VishalShah2
Occasional Contributor II

This code below works as intended turning off labeling for all layers. However, I cannot figure out a way to implement a variable I define earlier in the script as a global variable called layerList that contains the path to two layers I have in ArcMap so that the script can check against the variable layerList and only turn off labeling for those two particular feature classes.

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("LABELCLASSES"):
        if turnLabelsOnOff == True:
            layer.showLabels = True
        elif turnLabelsOnOff == False:
            layer.showLabels = False
0 Kudos
RandyBurton
MVP Regular Contributor

I have used code like:

mxd = arcpy.mapping.MapDocument("CURRENT")

LayerList = ['MyLayer']

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("LABELCLASSES"):
        if lyr.name in LayerList:
            lyr.showLabels = True
        
arcpy.RefreshActiveView()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
VishalShah2
Occasional Contributor II

Hi Randy,

Thank you for input on this matter. I went ahead and tried the code you provided but it did not work as intended. Also when you pass in the list (layerList), for file location when you print it out it gives two slashes instead of one that the normal file path has. Any other help would be greatly appreciated on this matter.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

"c:\\temp\\afile.txt" == r"c:\temp\afile.txt" == "c:/temp/afile.txt"  for the file name and path portion of your question.

a backslash is used to precede and escape character like a tab or new line ("\t" "\n") so to prevent a character being interpreted as an escape character, you need an additional backslash.  This behaviour is well documented in the python literature and is not unique to the Arc* line of products that employ python.

VishalShah2
Occasional Contributor II

Thank you for the clarification on the double backslash Dan.

0 Kudos
RandyBurton
MVP Regular Contributor

Perhaps you need to reference the "datasetName" or "dataSource" instead of the layer "name".  Which to use may depend on whether you plan to use your tool inside or outside of ArcMap.  See this help topic for explanations  of these different properties.

In code, perhaps you should try "datasetName" or "dataSource".  (Since you mentioned an issue with backslashes, perhaps "dataSource" would be the one to try.)

LayerList = [r'c:\pathTo\geo.gdb\LayerSource', 'anotherLayer']

for lyr in arcpy.mapping.ListLayers(mxd):
  if lyr.supports("LABELCLASSES"):
    if lyr.dataSource in LayerList:
      lyr.showLabels = True
    else:
      lyr.showLabels = False‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
VishalShah2
Occasional Contributor II

So here I'm testing it with showLabels being false to turn the labels off (default being Labels turned on). And this is was I passed in but it did not turn labels off for the feature classes x and y

x = 'location of first feature class in the mxd'
y = 'locaion of the second feature class in the mxd'
layerList = [x, y]
mxd = acpy.mapping.MapDocument("CURRENT")

for lyr in arcpy.mapping.ListLayers(mxd):
     if lyr.supports("LABELCLASSES"):
         if lyr.datasetName in layerList:
             lyr.showLabels = False
             
arcpy.RefreshActiveView()
0 Kudos
RandyBurton
MVP Regular Contributor

This code may help with the debugging process by printing the property values.

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("LABELCLASSES"):
        print "\n\nLayer name: " + lyr.name
        print " Show Layer: " + str(lyr.showLabels)
        print "   Data Set: " + lyr.datasetName
        print "Data Source: " + lyr.dataSource
0 Kudos
by Anonymous User
Not applicable

Vishal,

Instead of referencing the variables via their quoted name, reference the layers as such:

x = arcpy.mapping.ListLayers(mxd, "X")[0]
y = arcpy.mapping.ListLayers(mxd, "Y")[0]
layerList = [x, y]‍‍‍‍‍‍‍‍‍‍

One variation of the code that you have posted before will work as well now that the variables are being referenced correctly. 

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("LABELCLASSES"):
        if lyr in layerList:
            lyr.showLabels = False‍‍‍‍

You'll want to throw in your toggle variable as well, but that is just an extra if statement line just before the showLabels action.