0

I'm creating an addin that contains two comboboxes and a button, the latter of which adds data to the current map based on user selections in both comboboxes. Both comboboxes have pre-defined lists extracted from a folder of lyr files. If the user hasn't selected from either comboxes and clicks the button, I would like it to recognise this and show a dialogue box. Currently, if the button is clicked on startup, the global parameters litho and gde can't be defined. If items are selected from the comboboxes but then the text is deleted, then the button pressed, the last selection is remembered and previous data added to the map. I'd like the selection to reset to "" if the text is deleted in the box.

class fmb_toolAddLayers(object):
def __init__(self):
    self.enabled = True
    self.checked = False

def onClick(self):
    try:
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        gde_lyr_name = gde + " - FMB Paleo Map.lyr"
        litho_lyr_name = litho + " - FMB Lithopies.lyr"
        gde_layer = arcpy.mapping.Layer(os.path.join(gde_folder, gde_lyr_name))
        litho_layer = arcpy.mapping.Layer(os.path.join(litho_folder, litho_lyr_name))
        try:
            if not gde == "" and litho == "":
                arcpy.mapping.AddLayer(df, gde_layer, "TOP")
            elif gde == "" and not litho == "":
                arcpy.mapping.AddLayer(df, litho_layer, "TOP")
            elif not gde == "" and not litho == "":
                arcpy.mapping.AddLayer(df, gde_layer, "TOP")
                arcpy.mapping.AddLayer(df, litho_layer, "TOP")
        except Exception, ex:
            pythonaddins.MessageBox("Please select a layer(s) \n\n" + str(ex), "Error", 0)
        pass
        del mxd, gde_layer, litho_layer
    except Exception, ex:
        pythonaddins.MessageBox("Error adding layers to map \n\n" + str(ex), "Error")
        pass

# ------------------------------------------------------------------------------
#  TOOLBAR - COMBO BOXES
# ------------------------------------------------------------------------------

class fmb_comboGDE(object):
    def __init__(self):
        for root, dirs, files in os.walk(gde_folder):
            gde_files = [f.replace(" - FMB Paleo Map.lyr", "") for f in files]
        self.items = gde_files
        self.editable = False
        self.enabled = True
        self.dropdownWidth = 'WWWWWWWWWWWW'
        self.width = 'WWWWWWWWWWWW'
    def onSelChange(self, gde_selection):
        global gde
        gde = gde_selection
    def onEditChange(self, text):
        pass
    def onFocus(self, focused):
        pass
    def onEnter(self):
        pass
    def refresh(self):
        pass

class fmb_comboLithopies(object):
    def __init__(self):
        for root, dirs, files in os.walk(litho_folder):
            litho_files = [f.replace(" - FMB Lithopies.lyr", "") for f in files]
        self.items = litho_files
        self.editable = False
        self.enabled = True
        self.dropdownWidth = 'WWWWWW'
        self.width = 'WWWWWW'
    def onSelChange(self, litho_selection):
        global litho
        litho = litho_selection
    def onEditChange(self, text):
        pass
    def onFocus(self, focused):
        pass
    def onEnter(self):
        pass
    def refresh(self):
        pass
2
  • 1
    I've not done much with python addins but just a quick idea, why not startup the addin with the button disabled so they can never click on it until they have set both comboboxes? The code within these could be used to enable/disable the button?
    – Hornbydd
    May 14, 2020 at 13:51
  • That's actually a good idea, thanks!
    – Howeitzer
    May 15, 2020 at 7:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.