Arcpy
In this part, we'll adapt our model code from the last practical to work as an addin.
Copy the code from last practical into the onClick
function of your button addin. This will need some adaptation to work.
Note that each time you change the addin code, you'll need to rebuild the addin.esriaddin and run the install addin wizard. You'll also need to restart ArcMap.
First, make sure you import any libraries like traceback you might have used last practical; the best place for these are the top of the code.
Now, if we called the model as a geoprocessing tool directly, we'd no longer have the built in parameters that we can set
up through a script's properties. Unfortunately, GUI libraries like TkInter don't work easily in Arc
(details and workarounds), so
we can't easily build our own GUI to get this information. Fortunately, the pythonaddins
module provides us with a number of options. We could
ask the user for each piece of information using a
OpenDialog(). However,
we'd need several of these, and add new filters etc. to make sure the user chose the write types.
Instead, as our tool is already set up with parameters, the simplest option is to open the appropriate dialog box with:
object = pythonaddins.GPToolDialog(toolboxpath, tool_name)
This will just run the tool for us with our old dialog box. As this gives the Toolbox to use explicitly, you can get rid of the toolbox import code (note, however, that
even if the toolbox is loaded manually into Arc, you still need to give the full path in GPToolDialog – the name is not enough). Give this a go. If you manage to
hang ArcMap, you can always kill it with Windows Task Manager (right-click Windows taskbar at the bottom of the screen -> Start Task Manager -> select Arc Map in the
Applications tab -> push End Task).
What we don't have is the Python Window to output messages to. Can you adapt the code from last time to
output messages using
MessageBox()
? Note that this takes some
thought, as running the tool is enough for the code to move on – the tool doesn't have to finish. This means that message boxes you think should appear
after the tool has finished may appear at the same time as the tool is onscreen. Experiment with this and make a note of it: we'll come back to this issue next
practical when we look at processing the results of such tools.
Now, this may seem like a lot of effort to go to for a button, and, indeed it is if we just want to run one tool. However, the power of addins comes with:
- More sophisticated GUI elements, such as drop-down comboboxes.
- The ability to communicate between addins, linking several GUI elements together.
- The ability to respond to Arc events beyond the GUI with application extensions.
- Access to the elements of the pythonaddins module (as with other script tools).
In total, these make addins significantly more useful than standard scripts.
If you've got some time left, have a go at making a button addin that writes "Hello World" to a combobox (main thing to remember is that if your combobox has the ID
addin_addin.combobox
you just refer to it as combobox
in the onClick
); note also that you may have to scroll down the
box to see additions if you append to the combobox.items
.