Next / Previous / Contents / Shipman's homepage

3. Using the FontSelect widget in your Tkinter application

If you are writing a Tkinter application and you want to let the user change one or more fonts, you can include a FontSelect widget in your application. Create it with this constructor:

w = FontSelect(master, font=f, listCount=L, observer=F)
w

The constructor returns a new, ungridded FontSelect widget.

master

The parent Frame widget in which this widget will be mastered.

font=f

If you don't like the font used inside this widget, supply this optional keyword argument with f set to your preferred font as a tkFont.Font object.

listCount=L

By default, the number of families displayed in the family Listbox is defined in Section 5.2.1, “LISTBOX_HEIGHT. If you want a different number, pass that number as an integer L to this optional keyword argument. This Listbox pretty much determines the overall height of the FontSelect widget on the screen.

observer=F

An “observer” function to be called whenever any of the currently displayed font options change. The calling sequence is:

    F(newFont)

where newFont is a Python tkFont.Font object representing the currently selected font.

Here are the public attributes and methods of a FontSelect object:

.scrollList

A ScrollList widget containing the family names and associated scrollbar.

.get()

Returns the currently selected font as a Python tkFont.Font object. If no font has ever been set, returns None.

.getName()

Returns a string describing all the current font options. This string starts with the family and size, followed optionally by the strings "bold" and/or "italic". Example: "luxi mono 14 bold italic". If no font has ever been set, returns None.

.addObserver(f)

Adds an observer function f that will be called whenever any of the font options change. The calling sequence of the f function is as described under the observer=f argument to the FontSelect constructor.