9.15. 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).

Figure 9.15, “Font Selection Dialog” illustrates the FontSelectionDialog display:

Figure 9.15. Font Selection Dialog

Font Selection Dialog

The dialog contains a set of three notebook pages that provide:

The function to create a FontSelectionDialog is:

  fontseldlg = gtk.FontSelectionDialog(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 gtk.VBox containing the fontsel and the action_area in the dialog. The action_area attribute is a reference to the gtk.HButtonBox that contains the OK, Apply and Cancel buttons. The ok_button, cancel_button and apply_button attributes provide references to the OK, Apply and Cancel 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(fontname)

The fontname 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 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-110 define a callback that retrieves the font name from the Font Selection Dialog and uses it to set the font for the calendar widget. Lines 112-131 defines the method that creates the Font Selection Dialog, sets up the callbacks for the OK and Cancel buttons and displays the dialog.