It’s been said that tkinter’s strongest points may be its
Canvas widgets. Both provide a remarkable
amount of functionality. For instance, the tkinter
Text widget was powerful enough to implement
the web pages of Grail, an experimental web browser coded in Python;
Text supports complex font-style
settings, embedded images, unlimited undo and redo, and much more. The
Canvas widget, a general-purpose drawing device, allows for efficient
free-form graphics and has been the basis of sophisticated
image-processing and visualization applications.
In Chapter 11, we’ll put these two widgets to use to implement text editors (PyEdit), paint programs (PyDraw), clock GUIs (PyClock), and image programs (PyPhoto and PyView). For the purposes of this tour chapter, though, let’s start out using these widgets in simpler ways. Example 9-10 implements a simple scrolled-text display, which knows how to fill its display with a text string or file.
Example 9-10. PP4E\Gui\Tour\scrolledtext.py
"a simple text or file viewer component" print('PP4E scrolledtext') from tkinter import * class ScrolledText(Frame): def __init__(self, parent=None, text='', file=None): Frame.__init__(self, parent) self.pack(expand=YES, fill=BOTH) # make me expandable self.makewidgets() self.settext(text, file) def makewidgets(self): sbar = Scrollbar(self) text = Text(self, relief=SUNKEN) sbar.config(command=text.yview) # xlink sbar and text text.config(yscrollcommand=sbar.set) # move one moves other sbar.pack(side=RIGHT, ...