Quantcast
Channel: Question and Answer » arcmap
Viewing all articles
Browse latest Browse all 248

Creating Python Addin Button?

$
0
0

I want to create a Python AddIn button to execute a specific script. I know how to use the python addIn wizard and add the toolbar I created in my ArcMap. The trouble I am having is writing the script itself.

I want my script to pull a specific table into the ArcMap document. Then, I want it to add a field to that table. Then, I want it to run a for loop that updates that new empty field based on the values of another field. I have this script already written for a specific table in a personal geodatabase that I just run with IDLE, but wanted to know how I could implement this as a AddIn button.

Below is the generic script that you edit before adding it as a toolbar.

import arcpy  
import pythonaddins

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

This is what I have below. Right now I can’t even get the button to show up, it just says missing. The code I have written below works as its own script, but trying to get it to do what I want as a button.

import arcpy
import pythonaddins

class Button(object):

"""Implementation for NewAddIn_addin.button (Button)"""
def __init__(self):
    self.enabled = True
    self.checked = False
def onClick(self):
    arcpy.env.workspace = r"C:UserssuttonDocumentsPythonPythonExcercisePlanoData.mdb"
    try:
        #create new field to hold new values
        arcpy.AddField_management("SmokeMain","yesorno","TEXT","10")
        print("Field added to SmokeMain")
        with arcpy.da.UpdateCursor("SmokeMain",["Surface_Cover","yesorno"]) as cursor:
            cntr = 1
            for row in cursor:
                #update the indicator field
                if row[0] == "CONCRETE":
                        row[1] = "YES"
                else:
                        row[1] = "NO"
                cursor.updateRow(row)
                print ("Record number {} updated".format(cntr))
                cntr = cntr + 1

    except Exception as e:
        print(e.message)

       pass

Viewing all articles
Browse latest Browse all 248

Trending Articles