Reusable GUI Components with Classes
Larger GUI interfaces are often built up as subclasses
of Frame, with callback handlers
implemented as methods. This structure gives us a natural place to
store information between events: instance attributes record state. It
also allows us to both specialize GUIs by overriding their methods in
new subclasses, and attach them to larger GUI structures to reuse them
as general components. For instance, a GUI text editor implemented as
a Frame subclass can be attached to
and configured by any number of other GUIs; if done well, we can plug
such a text editor into any user interface that needs text editing
tools.
We’ll meet such a text editor component in Chapter 12. For now, Example 8-20 illustrates the concept in a simple way. The script gui6.py produces the window in Figure 8-22.

Figure 8-22. A custom Frame in action
Example 8-20. PP3E\Gui\Intro\gui6.py
from Tkinter import *
class Hello(Frame): # an extended Frame
def _ _init_ _(self, parent=None):
Frame._ _init_ _(self, parent) # do superclass init
self.pack( )
self.data = 42
self.make_widgets( ) # attach widgets to self
def make_widgets(self):
widget = Button(self, text='Hello frame world!', command=self.message)
widget.pack(side=LEFT)
def message(self):
self.data += 1
print 'Hello frame world %s!' % self.data
if _ _name_ _ == '_ _main_ _': Hello().mainloop( )This example pops up a single-button ...