Debugging ArcGIS Tools
You are about to learn how to create Tools for ArcGIS based on Python scripts. One of the challenges with this is that there is no "Debug I/O" window within ArcGIS to help us in debugging the scriopts. There are a number of approaches to help with debugging:
- Using Python dialog boxes to output messages
- Outputing messages to the result dialog with arcpy.AddMessage()
- Developing code outside of ArcGIS (turning of the interface code)
- Writing to a log file
We'll look at the first two below and then touch on the others a little later.
Tkinter
Tkinter is one of the packages for Python to display Graphical User Interfaces (GUIs). Tkinter is included with Python and is very easy to use to display simple dialog boxes. Tkinter also allows other types of windows and can be used to create complex GUIs.
Note: Python is a scripting language and not a compiled language. This does limit the performance of Python scripts. If you want to build a high performance application you will want to either switch to a compiled language like Java or use compiled packages with Python for much of the application's funcionality.
Tkinter by default will put up a modeless window typical of most desktop applications. We only want to put up a dialog box (also known as a message box) at this point so the first thing we're going to do is turn off the modeless window and then we'll put up a dialog with a message. Try the code below.
import tkMessageBox # import the message box module # before showing the dialog box, we'll want to turn off the rest of the GUI import Tkinter # import the GUI package root=Tkinter.Tk() # get the root object of the GUI root.withdraw() # turn off the typical modeless window in the GUI tkMessageBox.showinfo("Dialog Title","Message within the dialog") # show the dialog box
You should see the message in the moddle of the box which should disappear when you click 'OK'. When you call the "showinfo()" function, the function will wait for the user to click the "OK" button. The "tkMessageBox" allows you to display a vareity of message boxes including warnings and errors.
Imaging what you can put into the string in the message box! This could be a message to your users, a message to you than an error has occurred, or it could be debugging information.
Outputing Messages to the Results Dialog in ArcGIS
You can also add messages to the "Results Dialog" that appears when you run a custom tool in ArcGIS. These messages are also available through the "Results" item in the "Geoprocessing" menu. This is done by simply calling "AddMessage()" as below:
arcpy.AddMessage("Put the message here")
You'll want to use the dialog boxes when you only have a few messages and AddMessage() when you have a lot. The dialogs can be a pain if you have a lot of them to click "OK" on but the AddMessage() calls can get lost in many lines of output in the Results Dialog. Another trick is to call AddMessage() with something you can see easily in the output like:
arcpy.AddMessage("-------------------------") arcpy.AddMessage("Put the message here") arcpy.AddMessage("-------------------------")
Additional Resources
Python Documentation: Tkinter