0

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")
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Apr 12, 2016 at 14:50
0

1 Answer 1

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__))
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Apr 25, 2016 at 15:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.