Leave a comment

Adding a Python Script to a new Toolbar in ArcView 9.3.1. tutorial

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 to the Toolbars tab. If you do not want to add your own Toolbar, skip this section.
  • Click New for a new toolbar:

  • Name your 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:

Dim pUID As New UID
pUID = "esriGeoprocessingUI.ArcToolboxExtension"
Dim pGP As IGeoProcessor
Set pGP = New GeoProcessor
Dim pATBExt As IArcToolboxExtension
Set pATBExt = Application.FindExtensionByCLSID(pUID)
Dim pAtb As IArcToolbox
Set pAtb = pATBExt.ArcToolbox
Dim pTool As IGPTool
Set pTool = pAtb.GetToolbyNameString("mypythontoolname")
Dim pCommand As IGPToolCommandHelper
Set pCommand = New GPToolCommandHelper
pCommand.SetTool pTool
pCommand.Invoke Nothing</span>

  • 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!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: