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.

fontselect.py
class FamilyPicker(tk.Frame):
    """Widget to select a font family.

      Exports:
        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.

fontselect.py
      Internal widgets:
        .topLabel:  [ a Label above the family list ]
        .scrollList:
          [ a ScrolledList containing the family names ]
    """

6.1. FamilyPicker.__init__(): Constructor

As with any compound widget, we start by calling the parent class constructor.

fontselect.py
    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.

fontselect.py
        #-- 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.

fontselect.py
        #-- 2 --
        self.fontSelect = fontSelect
        self.observer   = observer

        #-- 3 --
        # [ self  :=   self with a new Label widget added
        #              and gridded
        #   self.topLabel  :=  that widget ]
        self.topLabel = tk.Label(self,
            font=fontSelect.regularFont,
            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,
            callback=self._listboxHandler)
        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.

fontselect.py
        #-- 5 --
        # [ self.scrollList  :=  self.scrollList with a list of
        #       the font families from tkFont added ]
        familySet = list(tkFont.families())
        familySet.sort()
        for  name in familySet:
            self.scrollList.append(name)