I have a toolbar (in ArcGIS) set up with two buttons using python addin manager, can someone help me make sure this works, when I run these buttons nothing happens.
import arcpy
import pythonaddins
import os
class ButtonClass1(object):
"""Implementation for ReconTB_addin.button (Button)"""
def __init__(self):
self.enabled = True
self.checked = True
def onClick(self):
os.startfile('subprocess.py')
class ButtonClass2(object):
"""Implementation for ReconTB_addin.button_1 (Button)"""
def __init__(self):
self.enabled = True
self.checked = True
def onClick(self):
mxd = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListDataFrames(mxd)[0]
addLayer = arcpy.mapping.Layer(r"mypath/sample.shp")
arcpy.mapping.AddLayer(df, addLayer, "TOP")
1 Answer 1
I'm not sure you can use startfile to launch a python file. You need to ensure the environment variables are setup. Why not just package it with your addin and import it to call the function directly?
Also, Layer() doesn't accept a shapefile only lyr files. You need MakeFeatureLayer to do that. If the shapefile is packaged with the addin you need to find the current path of the addin to append it to the name of the shp. Otherwise use pythonaddin openfiledialog.
For your reference, this code snippet returns the current path of the python file it is in:
os.path.dirname(os.path.realpath(__file__))