From time to time we like to feature guest posts. Today I’d like to draw your attention to a tutorial from Bob Roberts at the Python and Then Some blog. We’re going to be re-posting several of Bob’s tutorials. I think you’ll find them quite helpful. The first post will teach you how to add a Python script to a toolbar in ArcView 9.3.1. You could easily apply the same concept to ArcGIS 10.0. And now…more from Bob.
So you finished your Python ArcToolbox tool and now you want to add a shortcut to the toolbar. This tutorial is based on the assumption that you have already added your script to the ArcToolbox tools. If not I will cover that in another tutorial. I will show how to add a button in addition to how to add a menu to ArcView toolbar. Both are convenient ways to launch your Python script tools without having to open ArcToolbox.
First we will create a button to launch your tool.
- Go to Tools > Customize. This will open up the Toolbars tab. If you do not want to add your own Toolbar, skip this section.
Save it to either the current project (the .mxd) or to the Normal.mxt for use in all projects.
Now that you have a new and empty toolbar we need to create the button for the Python tool.
- Scroll to the bottom of the Categories window:
- Highlight [UIControls] and at the bottom click New UIControl
- Select UIButtonControl and hit Create
Now that the button is one the toolbar, we need to set it up to launch your Python tool.
- Right click on the new button and you should see several options here.
- You can change the button image to whatever you would like or you can just use “Text Only” for the name of the tool.
Now that we have the button on the toolbar and changed the way that it displays, it is time to add the Visual Basic snippet to launch the tool.
- Click on “View Source”. This should open up the Microsoft Visual Basic editor:
- Copy and Paste the following script between the Private Sub UIButtonControl1_Click() and End Sub:
- You will need to replace “mypythontoolname” with the name of your Python tool.
- Click the save button at the top of the VB editor.
- Click on your button and it should open
There are a few things that can cause the button not to open your Python tool. The most common that I have seen is that the Visual Basic reference is not selected as available for using the ESRI GeoprocessingUI Object Library. This can be added by going into the Visual Basic editor again. At the top there is an option for Tools and then select references. Make sure that the ESRI GeoprocessingUI Object Library is checked. If not, check it to make the library available.
If you want to add a drop down menu for listing multiple Python tools the steps are mostly the same. After creating a new toolbar, you can create a new menu before you create the tool buttons.
On the Customize > Commands tab in the left hand pane there is an option for [New Menu]. Create the new menu in the same manner as creating the UIControls button. Add the menu to the toolbar and then you can add tool buttons to the drop down menu by clicking and dragging them to the drop down.
Hope that helps!
Additional Python and ArcGIS tutorials:
- Using ArcPy Mapping to Publish Map Documents to ArcGIS Server
- Print and Export Maps from ArcMap with ArcPy Mapping
- Using ArcPy Mapping to Add and Remove Layers in a Map Document
- Working with Layout Elements in the ArcPy Mapping Module
- Introducing the ArcPy.Mapping Module in ArcGIS 10
- Free E-Learning Module from the ArcGIS 10 Version of our GIS Programming 101 Course
- Scripting Your ArcGIS Geoprocessing Tasks (Part 1)
- Scripting Your ArcGIS Geoprocessing Tasks with Cursors (Part 2)
- More ArcGIS Geoprocessing Concepts – Enumeration Objects (Part 3)
- Obtaining Descriptive Information About GIS Datasets with ArcGIS Geoprocessing Objects (Part 4)
- Executing ArcGIS Tools from Geoprocessing Scripts (Part 5)
The next session of our instructor guided, Internet based course Programming 101 for ArcGIS 10: Mastering Python begins February 21st, 2011. We still have a few seats available. A self-paced version of this course is also available.