Archive for February, 2011

Adding a Python Script to a New Toolbar in ArcView 9.3.1 (Tutorial)

Posted on February 10, 2011. Filed under: Geoprocessing, Python |

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.
  • Click New for new toolbar:
  • Save it to either the current project (the .mxd) or to the Normal.mxt for use in all projects.
  • This will create an empty toolbar. Drag the toolbar to the top of ArcView.

Now that you have a new and empty toolbar we need to create the button for the Python tool.

  • Click on the Commands tab under Customize:
  • Scroll to the bottom of the Categories window:
  • Highlight [UIControls] and at the bottom click New UIControl
  • Select UIButtonControl and hit Create
  • Click in the box for the name if you wish to rename the tool
  • Next, click and drag the new UIButtonControl1 to your toolbar that was just created.

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:

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.

Read Full Post | Make a Comment ( None so far )

Liked it here?
Why not try sites on the blogroll...