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– fmark2010年07月28日 00:10:52 +00:00Commented Jul 28, 2010 at 0:10
-
no, not through a toolbox. that takes up too much screen real estate.matt wilkie– matt wilkie2010年07月28日 14:18:26 +00:00Commented Jul 28, 2010 at 14:18
-
arcmap 10 version of this question: gis.stackexchange.com/questions/1697/…matt wilkie– matt wilkie2011年10月04日 22:26:38 +00:00Commented Oct 4, 2011 at 22:26
3 Answers 3
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.
-
2this 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– matt wilkie2010年07月28日 18:16:17 +00:00Commented Jul 28, 2010 at 18:16
-
What about .net? Any code snippets?George Silva– George Silva2010年07月28日 19:04:51 +00:00Commented Jul 28, 2010 at 19:04
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:
- You can call the script as a command line argument from another program that is linked to the button - the easiest would be VBA.
- 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.
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– matt wilkie2011年04月12日 17:37:40 +00:00Commented Apr 12, 2011 at 17:37