self.text.focus() # select text widget
if name == 'main':
if len(sys.argv) > 1:
SimpleEditor(file=sys.argv[1]).mainloop() # filename on command line
else:
SimpleEditor().mainloop() # or not: start empty
This, too, was written with one eye toward reuse—the SimpleEditor class it defines
could be attached or subclassed by other GUI code. As I’ll explain at the end of this
section, though, it’s not yet as robust as a general-purpose library tool should be. Still,
it implements a functional text editor in a small amount of portable code. When run
standalone, it brings up the window in Figure 9-19 (shown editing itself and running
on Windows); index positions are printed on stdout after each successful find
operation—here, for two “def” finds, with prior selection removal logic commented-
out in the script (uncomment this line in the script to get single-selection behavior for
finds):
C:\...\PP4E\Gui\Tour> python simpleedit.py simpleedit.py
PP4E scrolledtext
14.4
25.4
Figure 9-19. simpleedit in action
The save operation pops up the common save dialog that is available in tkinter and is
tailored to look native on each platform. Figure 9-20 shows this dialog in action on
Windows 7. Find operations also pop up a standard dialog box to input a search string
Text | 535