Next / Previous / Contents / Shipman's homepage

6. class FamilyPicker: Widgets for selecting the font family

This class is a compound widget that allows the user to select a font family. It inherits from Frame so that it can act as a widget.
class FamilyPicker(tk.Frame):
    """Widget to select a font family.

        FamilyPicker(fontSelect, observer=None):
          [ (fontSelect is a FontSelect widget) and
            (observer is a function or None) ->
              fontSelect  :=  fontSelect with a new FamilyPicker
                  widget added but not gridded, with that
                  observer callback if given
              return that FamilyPicker widget ]

This class has only two sub-widgets: a label for the list, and a ScrolledList widget that contains the list of family names.
      Internal widgets:
        .topLabel:  [ a Label above the family list ]
          [ a ScrolledList containing the family names ]

6.1. FamilyPicker.__init__(): Constructor

As with any compound widget, we start by calling the parent class constructor.
    def __init__(self, fontSelect, observer):
        """Constructor for FamilyPicker

Note that we call the parent frame argument fontSelect and not master as is conventional. We need the parent to be a Frame, but we need it specifically to be a FontSelect widget, because we'll need to use its font attributes .regularFont and .listFont.
        #-- 1 --
        # [ fontSelect  :=  fontSelect with a new Frame added
        #   self  :=  that Frame ]
        tk.Frame.__init__(self, fontSelect)

The grid plan is quite simple: the label is in row 0, and the family list is in row 1.
        #-- 2 --
        self.fontSelect = fontSelect   = observer

        #-- 3 --
        # [ self  :=   self with a new Label widget added
        #              and gridded
        #   self.topLabel  :=  that widget ]
        self.topLabel = tk.Label(self,
            text="Click to select font family:")
        self.topLabel.grid(row=0, column=0, sticky=tk.W)

        #-- 4 --
        # [ self  :=  self with a new ScrolledList widget added
        #             and gridded
        #   self.scrollList  :=  that widget ]
        self.scrollList = scrolledlist.ScrolledList(self,
            width=LISTBOX_WIDTH, height=LISTBOX_HEIGHT,
        self.scrollList.grid(row=1, column=0)
        self.scrollList.listbox["font"] = fontSelect.listFont

Now that we have a listbox for the font families, we need to load in the list of families. The tkFont.families() function returns an unsorted tuple of the family names. We'll convert that to a list, sort them, then load them into the listbox.
        #-- 5 --
        # [ self.scrollList  :=  self.scrollList with a list of
        #       the font families from tkFont added ]
        familySet = list(tkFont.families())
        for  name in familySet: