1

I am trying to create a Python add-in for ArcMap for users to quickly select layer files from within several different folders. This will require 2 combo boxes. User selects the folder from the first combo box, then the layerfile from the second combo box. The second combo box is populated based on the folder selected in the first.

I have found a couple of questions on here similar to this: Using two Python Addin Combo Boxes to choose feature class first then field from chosen feature class? and Dynamically adding items into one python addin Combobox from another?

I have successfully copied and pasted PolyGeo's code for selecting feature class then field, but when I try and change the code to select folder then layer file, I can't get it to work. The first combo box displays the folders correctly, but the second combo box is blank. Can anyone help?

Code below. I am a beginner with ArcPy and add-ins.

import arcpy
import pythonaddins
class ComboBoxFolders(object):
 """Implementation for AddIns_addin.combobox (ComboBox)"""
 def __init__(self):
 arcpy.env.workspace = r"C:\\EditData\\Folders\\"
 self.items = arcpy.ListWorkspaces("*", "Folder")
 self.editable = True
 self.enabled = True
 self.dropdownWidth = 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW'
 self.width = 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW'
 def onSelChange(self, selection):
 ComboBoxLayers._hook.items = [x.name for x in arcpy.ListFiles(selection)]
class ComboBoxLayers(object):
 """Implementation for AddIns_addin.combobox (ComboBox)"""
 def __init__(self):
 self.items = []
 self.editable = True
 self.enabled = True
 self.dropdownWidth = 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW'
 self.width = 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW'
 ComboBoxLayers._hook=self
 def onSelChange(self, selection):
 mxd = arcpy.mapping.MapDocument("CURRENT")
 addLayer = arcpy.mapping.Layer(r"C:\\EditData\\Folders\\" + selection)
 df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
 arcpy.mapping.AddLayer(df, addLayer, "TOP")
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Aug 12, 2019 at 13:45

2 Answers 2

1

Wouldn’t it be a better user experience to use a Button that pops up an open dialog?

answered Aug 26, 2019 at 22:29
1
  • Thanks, I had a look at this and it's an option but not sure it's a better user experience - there's an extra few clicks needed that would add up if you were using it a lot. Commented Oct 27, 2020 at 16:10
0

I came back to this after a break and was able to get it to work, so posting in case it's useful for anyone else. This was the final code:

import arcpy
import pythonaddins
import os
class ComboBoxFolder(object):
 """Implementation for LayerAddIn_addin.combobox (ComboBox)"""
 def __init__(self):
 arcpy.env.workspace = r"C:\GIS\LayerFiles"
 self.items = arcpy.ListWorkspaces("*", "Folder")
 self.editable = True
 self.enabled = True
 self.dropdownWidth = 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW'
 self.width = 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW'
 def onSelChange(self, selection):
 arcpy.env.workspace = str(selection)
 layernames = arcpy.ListFiles()
 layerslist = []
 for layer in layernames:
 layerpath = os.path.join(arcpy.env.workspace, layer)
 layerslist.append(layerpath)
 ComboBoxLayer._hook.items = layerslist
class ComboBoxLayer(object):
 """Implementation for LayerAddIn_addin.combobox_1 (ComboBox)"""
 def __init__(self):
 self.items = []
 self.editable = True
 self.enabled = True
 self.dropdownWidth = 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW'
 self.width = 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW'
 ComboBoxLayer._hook=self
 def onSelChange(self, selection):
 mxd = arcpy.mapping.MapDocument("CURRENT")
 df = arcpy.mapping.ListDataFrames(mxd)[0]
 filepath = str(os.path.abspath(selection))
 addLayer = arcpy.mapping.Layer(filepath)
 arcpy.mapping.AddLayer(df, addLayer)
answered Oct 27, 2020 at 16:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.