TransWikia.com

Using two Python Addin Combo Boxes to choose Folder then Layer

Geographic Information Systems Asked by earthwriter on February 27, 2021

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:EditDataFolders"
        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:EditDataFolders" + selection)
        df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
        arcpy.mapping.AddLayer(df, addLayer, "TOP")

2 Answers

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

Correct answer by earthwriter on February 27, 2021

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

Answered by Jason Scheirer on February 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP