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")

bumped to the homepage by Community 2 hours ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.