Programming ArcGIS 10.1 with Python Cookbook
上QQ阅读APP看书,第一时间看更新

Using the ArcGIS Python window

In this recipe, you'll learn how to use the ArcGIS Python window. In Chapter 1, Fundamentals of the Python Language for ArcGIS, you learned how to use the IDLE development environment for Python, so this chapter will give you an alternative for writing your geoprocessing scripts. Either development environment can be used but it is common for people to start writing scripts with the ArcGIS Desktop Python window and then move on to IDLE when scripts become more complex. I should also mention that there are many other development environments that you may want to consider, including PythonWin, Wingware, Komodo, and others. The development environment you choose is really a matter of preference.

Getting ready

The new Python window is an embedded, interactive Python window in ArcGIS Desktop 10, that is ideal for testing small blocks of code, learning Python basics, building quick and easy workflows, and executing geoprocessing tools. However, as your scripts become more complex, you'll quickly find the need for a more robust development environment. By default, IDLE is installed with ArcGIS Desktop, so this is a logical next choice for many. For new programmers, though, the ArcGIS Python window is a great place to start!

The ArcGIS Python window has a number of capabilities in addition to being the location for writing your code. You can save the content of the window to a Python script file on a disk or load an existing Python script into the window. The window can be either pinned or floating. While floating, the window can be expanded or contracted as you wish. The window can also be pinned to various parts of the ArcGIS display. You can also format the font and text colors displayed in the window by right-clicking on the window and selecting Format.

How to do it…

The Python window can be opened by clicking on the Python window button on the main ArcGIS Desktop toolbar.

  1. Open c:\ArcpyBook\Ch2\Crime_Ch2.mxd with ArcMap.

    Note

    You don't have to specifically open Crime_Ch2.mxd. Any map document file can be used with the Python window.

  2. Click on the Python window button from the main ArcMap toolbar. The Python window will be displayed as shown in the following screenshot. This is a floating window, so you can resize as needed and also dock it at various places on the ArcMap interface:
    How to do it…

    The Python window is essentially a shell window that allows you to type in statements one line at a time, just after the line input characters >>>. On the right-hand side of the divider, you will find a help window.

  3. Load an existing script by right-clicking inside the Python window and selecting Load from the menu. Navigate to the c:\ArcpyBook\Ch2 directory and select ListFields.py to load a sample script.

    You can also format the font and text colors displayed in the window by right-clicking on the window and selecting Format. You will be provided with White and Black themes; you can select fonts and colors individually:

    How to do it…

Click on the Set Black Theme button to see an example. If you spend a lot of time writing code, you may find that darker themes are easier on your eyes:

How to do it…