Next / Previous / Contents / Shipman's homepage

5.4. FontSelect.__init__(): Constructor

The constructor for a class that inherits from Frame is pretty stereotyped: call the parent class's constructor, then create the widgets.

fontselect.py
    def __init__(self, master=None, font=None, listCount=None,
                 observer=None):
        """Constructor for the FontSelect widget.
        """

        #-- 1 --
        # [ master  :=  master with a new Frame widget added
        #   self    :=  that Frame ]
        tk.Frame.__init__(self, master, relief=tk.RIDGE,
                          borderwidth=4)

We export the usual font for controls as the .regularFont attribute.

fontselect.py
        #-- 2 --
        self.regularFont = tkFont.Font (
            family="new century schoolbook", size="13")

Next, we process the various constructor arguments. If a font was passed in, we store it in self.listFont; the default value is the regular font.

fontselect.py
        #-- 3 --
        # [ if font is None ->
        #     self.listFont  :=  self.regularFont
        #   else ->
        #     self.listFont  :=  font ]
        self.listFont  = font or self.regularFont

        #-- 4 --
        # [ if listCount is None ->
        #     self._listCount  :=  LISTBOX_HEIGHT
        #   else ->
        #     self._listCount  :=  listCount ]
        self._listCount = listCount or LISTBOX_HEIGHT

If an observer function was passed in, put it in self._observerList.

fontselect.py
        #-- 5 --
        # [ if observer is not None ->
        #     self._observerList  :=  [ observer ]
        #   else ->
        #     self._observerList  :=  an empty list ]
        self._observerList = []
        if observer:
            self._observerList.append(observer)

For widget creation, see Section 5.5, “FontSelect._createWidgets(): Widget placement”.

fontselect.py
        #-- 6 --
        # [ self  :=  self with all widgets and control linkages ]
        self._createWidgets()