16.3. Font Selection Dialog

The Font Selection Dialog allows a user to interactively select a font for use within your program. The dialog contains a FontSelection widget and "OK and "Cancel" buttons. An 'Apply' button is also available in the dialog but is initially hidden. The Font Selection Dialog allows a user to select a font from the available system fonts (the same ones that can be retrieved using xlsfonts)).
 
The dialog contains a set of three notebook pages that provide:
Figure 16.2 illustrates the FontSelectionDialog display:

Figure 16.2 Font Selection Dialog

The function to create a FontSelectionDialog is:
 
fontseldlg = GtkFontSelectionDialog(title)

The title is a string that will be used to set the titlebar text.

A Font Selection Dialog instance has several attributes:
 
    fontsel
    main_vbox
    action_area
    ok_button
    apply_button
    cancel_button

The fontsel attribute provides a reference to the Font Selection widget. main_vbox is a reference to the GtkVBox containing the fontsel and the action_area in the dialog. The action_area attribute is a reference to the GtkHButtonBox that contains the 'OK', 'Apply' and 'Cancel' buttons. The ok_button, cancel_button and apply_button attributes provide references to the 'OK', 'Cancel' and 'Apply' buttons that can be used to set connections to the button signals. The apply_button reference can also be used to show() the 'Apply' button.

You can set the initial font to be displayed in the fontseldlg by using the method:
 
fontseldlg.set_font_name(string)

The string argument is the name of a completely specified or partially specified system font. For example:
 
fontseldlg.set_font_name('-adobe-courier-bold-*-*-*-*-120-*-*-*-*-*-*')

partially specifies the initial font.

The currently selected font can be retrieved using the method:
 
font = fontseldlg.get_font()

The font name of the currently selected font can be retrieved using the method:
 
font_name = fontseldlg.get_font_name()

The Font Selection Dialog has a Preview area that displays text using the currently selected font. The text that is used in the Preview area can be set with the method:
 
fontseldlg.set_preview_text(text)

The preview text can be retrieved with the method:
 
text = fontseldlg.get_preview_text()

The calendar.py example program uses a Font Selection Dialog to select the font to display the calendar information. Lines 105-112 define a callback that retrieves the font from the Font Selection Dialog and uses it to set the style font for the calendar widget. Lines 114-133 defines the method that creates the Font Selection Dialog, sets up the callbacks for the 'OK' and 'Cancel' buttons and displays the dialog.