Next / Previous / Contents / Shipman's homepage

7.6. Controls.getName(): Get the current font name

This public method returns a string with up to four components: family name, size, and optional weight and slant. Example: "luxi mono 14 bold italic".

However, the font we have built may not be exactly what the user specified. Not all fonts are available in all sizes, for example. So we use the .actual() method on the font to retrieve the options actually in force.

fontselect.py
    def getName(self):
        """Return a string describing the current font's options.

          [ if self._currentFont is None ->
              return None
            else ->
              return a string describing the current font's options ]
        """

        #-- 1 --
        if self._currentFont is None:
            return None

        #-- 2 --
        # [ attrs  :=  a list containing self._currentFont's
        #       actual family name and actual size (as a string) ]
        attrs = [ self._currentFont.actual("family"),
                  str(self._currentFont.actual("size")) ]

        #-- 3 --
        # [ if self._currentFont is bold ->
        #     attrs  +:=  "bold"
        #   else -> I ]
        if self._currentFont.actual("weight") == tkFont.BOLD:
            attrs.append("bold")

        #-- 4 --
        # [ if self._currentFont is italic ->
        #     attrs  +:=  "italic"
        #   else -> I ]
        if self._currentFont.actual("slant") == tkFont.ITALIC:
            attrs.append("italic")

The .join() string method assembles this list into a string, with the pieces separated by one space.

fontselect.py
        #-- 5 --
        return " ".join(attrs)