3

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:\Users\sutton\Documents\Python\PythonExcercise\PlanoData.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
8
  • 2
    I think you're going to have to go through some trial-and-error and bring something a little more complete to the table. stack exchange sites are not code writing services. If you have a specific question about the implementation, feel free to edit the question to reflect that.
    – Paul H
    Commented Feb 23, 2016 at 20:11
  • 1
    What happened when you tried to install that test Python Addin? What happened when you tried to incorporate your code into the onClick function? If you are interested in commercial eLearning videos then this may be useful: discoverspatial.com/courses/…
    – PolyGeo
    Commented Feb 23, 2016 at 20:33
  • I appreciate your comment Paul. I was not asking for a hand out. I am in the process of trial and error. An amateur programming learning how to solve these types of problems. As soon as I get a button that actually does something, besides click, with the code I have written I will have more to bring to the table.
    – Colin
    Commented Feb 23, 2016 at 20:53
  • 2
    Are you asking how you can get your already written script to run with the click of the toolbar button? If so you can simply add your entire script under the onClick function in the button class.
    – nwduncan
    Commented Feb 23, 2016 at 21:49
  • Please check the newest edit. I am unsure if I went about it correctly.
    – Colin
    Commented Feb 23, 2016 at 22:25

2 Answers 2

2

There are several things that I think you should look at:

  1. "I can't even get the button to show up, it just says missing" suggests to me that your config.xml file has a different name for your button than your Python script.
  2. I would remove any try and except statements because they often mask the error messages that would help you see what is going wrong
  3. There seems to be a spurious pass on the end of your code that is also incorrectly indented
  4. Your class and def statements should not be indented to the same level.

What I think you should do is to try the Python script below but make sure that your button name used in this Python script and your config.xml matches.

Also, assuming that the toolbar opens, make sure that you have ArcMap's Python window open when you click the button because any error messages will print there. Be aware that once the AddField has been done once you are likely to get an error the next time you click the button because your "yesorno" field will already exist.

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:\Users\sutton\Documents\Python\PythonExcercise\PlanoData.mdb"
        #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
1

There are 2 common reasons for the [missing]:

  1. Your code has something wrong

  2. Your computer has had another version of Python installed so that this Python conflicted with that one combined with Arcgis

I am also studying Add-in with Python now. This is my individual experience in the way.

Your Answer

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

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