DekGenius.com
[ Team LiB ] Previous Section Next Section

3.5 The IDLE User Interface

IDLE is a graphical user interface for doing Python development, and is a standard and free part of the Python system. It is usually referred to as an Integrated Development Environment (IDE), because it binds together various development tasks into a single view.[4]

[4] IDLE is a corruption of IDE, named in honor of Monty Python member Eric Idle.

In short, IDLE is a GUI that lets you edit, run, browse, and debug Python programs, all from a single interface. Moreover, because IDLE is a Python program that uses the Tkinter GUI toolkit, it runs portably on most Python platforms: MS Windows, X Windows (Unix, Linux), and Macs. For many, IDLE is an easy-to-use alternative to typing command lines, and a less problem-prone alternative to clicking on icons.

3.5.1 IDLE Basics

Let's jump right into an example. IDLE is easy to start under Windows—it has an entry in the Start button menu for Python (see Figure 2-1); it can also be selected by right-clicking on a Python program icon. On some Unix-like systems, you may need to launch IDLE's top-level script from a command line or icon click—start file idle.pyw in the idle subdirectory of Python's Tools directory.[5]

[5] IDLE is a Python program that uses the standard library's Tkinter GUI toolkit to build the IDLE GUI. This makes IDLE portable, but also means that you'll need to have Tkinter support in your Python to use IDLE. The Windows version of Python does by default, but Linux and Unix users occasionally need to install the appropriate Tkinter support (see the installation hints in Appendix A for details).

Figure 3-3 shows the scene after starting IDLE on Windows. The Python Shell window at the bottom is the main window, which runs an interactive session (notice the >>> prompt). This works like all interactive sessions—code you type here is run immediately after you type it—and serves as a testing tool.

Figure 3-3. IDLE main window and text edit window
figs/lpy2_0303.gif

IDLE uses familiar menus with keyboard shortcuts for most of its operations. To make (or edit) a script under IDLE, open text edit windows—in the main window, select the File menu pulldown, and pick New window to open a text edit window (or Open... to edit an existing file). The window at the top of Figure 3-3 is an IDLE text edit window, where the code for file script3.py was entered.

Although this may not show up fully in this book, IDLE uses syntax-directed colorization for the code typed in both the main window, and all text edit windows—keywords are one color, literals are another, and so on. This helps give you a better picture of the components in your code.

To run a file of code that you are editing in IDLE, select the file's text edit window, pick that window's Edit menu pulldown, and choose the Run Script option there (or use the equivalent keyboard shortcut, listed in the menu). Python will let you know that you need to save your file first, if you've changed it since it was opened or last saved. (In Python 2.3, the menu structure changes slightly: the Run Module option in the new Run menu has the same effect as the prior version's Edit/Runscript menu selection. See the sidebar IDLE Changes in 2.3 later in this chapter.)

When run this way, the output of your script, and any error messages it may generate, shows up back in the main interactive window. In Figure 3-3, for example, the last two lines in the bottom interactive window reflect an execution of the script in the top text edit window.

Hint of the day: To repeat prior commands in IDLE's main interactive window, use Alt-P to scroll backwards through command history and Alt-N to scroll forwards. Your prior commands are recalled and displayed, and may be edited and rerun. You can also recall commands by positioning the cursor on them, or use cut-and-paste operations, but these tend to be more work. Outside IDLE, you may be able to recall commands in an interactive session with the arrow keys on Windows, if you're running doskey or using a recent version of Windows.


3.5.2 Using IDLE

IDLE is free, easy to use, and portable. But it is also somewhat limited compared to more advanced commercial IDEs. Here are a list of common issues that seem to trip up IDLE beginners:

  • You must add ".py" explicitly when saving your files. We mentioned this when talking about files in general, but it's a common IDLE stumbling block, especially for Windows users. IDLE does not automatically add a .py extension to file names when files are saved. Be careful to type the .py extension at the end of filenames yourself, when saving them for the first time. If you don't, you will be able to run your file from IDLE (and command lines), but will not be able to import your file either interactively or from other modules.

  • Make sure you're not looking at old error messages. IDLE currently does not do a good job of separating the output of each script execution in the interactive window—there is no blank line between the outputs. Because of that, many a beginner has been fooled by a prior run's error message into thinking that their script is still failing, when in fact it is silently succeeding. Make sure the error messages you see are not old—typing an empty line in the interactive window helps.

  • Run scripts from text edit windows, not the interactive window. To run a file of code under IDLE, always select the Edit/RunScript menu option from within the text edit window where you are editing the code to be run—not from within the main interactive window where the >>> prompt appears. The RunScript option should arguably not be available in the interactive window at all (and in fact seems to have disappeared in the recent release); if you select it there, you'll wind up trying to run a log of your interactive session, with less than desirable results!

  • Tkinter GUI programs may not work well with IDLE. Because IDLE is a Python/Tkinter program, it can be hung if you use it to run certain types of Python/Tkinter programs, especially if your code runs a Tkinter mainloop call. Your code may not exhibit such problems, but as a rule of thumb, it's always safe if you use IDLE to edit GUI programs, but launch them using other options such as clicks or command lines.

  • Other programs may not work well with IDLE either. More generally, because IDLE currently (in 2.2) runs your code in the same process that IDLE itself runs in, it's not impossible to hang IDLE with non-GUI programs as well. In fact, because IDLE uses neither processes nor threads to launch scripts, an infinite loop may render IDLE completely unresponsive. As a rule of thumb, if you can't find a reason for a program failure under IDLE, try running the program outside IDLE to make sure your problem is not really an IDLE problem.

    This may improve over time (see the sidebar IDLE Changes in 2.3). The upside to this structure today is that, because your script is run in IDLE's environment, variables in your code show up automatically in the IDLE interactive session—you don't always need to run import commands to access names at the top level of files you've already run.

  • Run scripts by Edit/Run Script, not imports and reloads. In the prior section, we saw that it's also possible to run a file by importing it interactively. However, this scheme can grow complex because you are required to manually reload files after changes. By contrast, the Edit/RunScript option in IDLE always runs the most current version of your file. It also prompts you to save it first, if needed—another common mistake outside IDLE.

    Technically speaking, IDLE's Edit/Runscript option always runs the current version of the top-level file only; imported files may still need to be interactively reloaded when changed. In general, though, Edit/Runscript eliminates common confusions surrounding imports. If you choose to use the import and reload technique instead, remember to use Alt-P/Alt-N key combinations to recall prior commands.

  • Customizing IDLE. To change the text fonts and colors in IDLE, edit the configuration files in IDLE's source directory. For example, for Python 2.2 on Windows, the file C:\Python22\Tools\idle\config-win.txt specifies text font and size. See file config.txt in that directory or IDLE's help pulldown menu for more hints. Also see the sidebar IDLE Changes in 2.3.

  • There is currently no clear-screen in IDLE. This seems to be a frequent request (perhaps because of similar IDEs), and might be added eventually. Today, though, there is no way to clear the interactive window's text. If you want the window's text to go away, you can press and hold the Enter key, or type a Python loop to print blank lines.

IDLE Changes in 2.3

Most of the cautions about IDLE in this section become moot in the new release, Python 2.3. A new version of IDLE, known initially as IDLEfork, was incorporated in Python 2.3 as the new IDLE, after this section was written. This new version fixes many of the limitations in the prior release.

For instance, the new IDLE runs your code in a separate process such that it will not disturb IDLE itself. This separate process is restarted for each run initiated from a text edit window. Because of this, interference problems when running GUIs or other code within IDLE should largely be a thing of the past.

In addition, the new IDLE changes the menu structure such that the original Edit/Run Script menu option is now in Run/Run Module, and only appears in the text edit window (it is no longer possible to inadvertently run the interactive session's text). The new IDLE also includes GUI-based configuration tools, which allow fonts and colors to be customized without editing text files, and does a much better job of separating the error message of one program run from the next. For more details, experiment with the 2.3 version of IDLE on your own.


Besides the basic edit and run functions, IDLE provides more advanced features, including a point-and-click program debugger and an object browser. Figure 3-4, for example, shows the IDLE debugger and object browser windows in action. The browser allows you to navigate through the module search path to files and objects in files; clicking on a file or object opens the corresponding source in a text edit window.

Figure 3-4. IDLE debugger and path/object browser
figs/lpy2_0304.gif

IDLE debugging is initiated by selecting the Debug/Debugger menu option in the main window, and then starting your script by selecting the Edit/Run Script option in the text edit window; once started, you can set breakpoints in your code that stop its execution by right-clicking on lines in the text edit windows, show variable values, and so on. You can also watch program execution when debugging—selecting the debugger's source toggle will cause the active line to be highlighted in the text edit window, as you step through your code.

In addition, IDLE's text editor offers a large collection of programmer-friendly tools, including automatic indentation, advanced text and file search operations, and more. Because IDLE uses intuitive GUI interactions, experiment with the system live to get a feel for its other tools.

    [ Team LiB ] Previous Section Next Section