7

In ArcMap 9.x how can I attach a python script to a button?

  • What do you mean by a button? The easiest way to integrate a python script into ArcMap is through Toolbox. Is that okay? – fmark Jul 28 '10 at 0:10
  • no, not through a toolbox. that takes up too much screen real estate. – matt wilkie Jul 28 '10 at 14:18
  • arcmap 10 version of this question: gis.stackexchange.com/questions/1697/… – matt wilkie Oct 4 '11 at 22:26
7

This works in VBA to run a Python script. Just call the subroutine with a button.

Private Sub python_Click()
    Shell "C:\Python25\python.exe ""C:\rowcount.py"
End Sub

Hope this helps.

  • 2
    this is a good example of why this stack exchange platform works. I had an answer ready, but this one is simpler and cleaner. Thanks :) – matt wilkie Jul 28 '10 at 18:16
  • What about .net? Any code snippets? – George Silva Jul 28 '10 at 19:04
5

As @fmark says, the easiest way to integrate a Python script into ArcMap is through a toolbox, which I'd recommend.

That said, if you really need to (eg, you need a nicer front end than the toolbox) you have two options:

  1. You can call the script as a command line argument from another program that is linked to the button - the easiest would be VBA.
  2. You can add the Python script to the toolbox, and call the custom toolbox from your button (VBA, VB.Net, C#.Net).

Still, I'd go with @fmark and just add the script to the toolbox given the choice.

1

The way to do it in C# would be like this using the ArcGIS template for a button.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace RunEditPython
   {    
    public class PythonEdited : ESRI.ArcGIS.Desktop.AddIns.Button
    {

    public PythonEdited()
    {
    }

    protected override void OnClick()
    {
        System.Diagnostics.Process.Start("C:\\temp\\WHP\\WellsEdited.py");
    }
    protected override void OnUpdate()
    {
        Enabled = ArcMap.Application != null;
    }
}

}

  • thanks! and welcome to GIS stack exchange. I hope you find your time here fruitful; you've certainly come bearing gifts, it's appreciated. – matt wilkie Apr 12 '11 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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