TransWikia.com

Python add-in: pass selected layer of combo box to a button

Geographic Information Systems Asked by BBG_GIS on June 25, 2021

I want to list layer names in a combo box and pass the selected layer name to onclick of a button.
I could list the layer names in combobox but i don’t know how to pass the name to a button. How can i solve the issue?

import arcpy
import pythonaddins

class ButtonClass2(object):
    """Implementation for test_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
            
        pass
        

class ComboBoxClass1(object):
    """Implementation for test_addin.combobox (ComboBox)"""
    def __init__(self):
        self.items = ["item1", "item2"]
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWW'
        self.width = 'WWWWWW'
    def onSelChange(self, selection):
        pass
    def onEditChange(self, text):
        pass
    def onFocus(self, focused):
         # When the combo box has focus, update the combo box with the list of layer names.
    if focused:
        self.mxd = arcpy.mapping.MapDocument('current')
        layers = arcpy.mapping.ListLayers(self.mxd)
        self.items = []
        for layer in layers:
            self.items.append(layer.name)
    def onEnter(self):
        pass
    def refresh(self):
        pass

One Answer

Each of the classes get instantiated by Arc's opaque machinery using the variable name hinted at in the docstring. Ignore the prefix since you're working within the same module that it's instantiated within.

So when you see

"""Implementation for test_addin.combobox (ComboBox)"""

it means that combobox (lowercase) is the instance available in the global scope and you can simply do

class ButtonClass2(object):
    """Implementation for test_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        layer = combobox.value

Note: if you have renamed the classes after the docstrings were generated they may no longer be accurate. The source of truth would be the id attributes in the config.xml.

Correct answer by mikewatt on June 25, 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