PyGTK - Font Selection Dialog


Advertisements

The gtk.FontSelection widget allows users to select and apply the font of a particular name, size and style. The dialog has a preview box containing some text which will be displayed in selected font description, and two buttons CANCEL and OK.

PyGTK API contains a Pango module which defines classes and functionality required to render high quality internationalized text. Font and text handling in gtk is supported by Pango. The pango.Font object represents a font in a system independent way. The pango.FontDescription object contains the characteristics of a font.

gtk.FontSelectionDialog returns a pango.Font object. In order to apply the selected font, fontmetrics is fetched by obtaining the pango.FontDescription object from it.

The following is a constructor of the FontSelectionDialog class −

dlg = gtk.FontSelectionDialog(title)

The following are some frequently used methods of this class −

  • get_font_name() − This returns a string containing the currently selected font name or None if no font name is selected.

  • set_font_name() − This sets the current font

  • set_preview_text() − This sets the text in the preview area entry

The selected font is applied to the text in a widget using the modify_font() method.

When FontSelectionDialog menu item is activated, the following callback function is invoked −

def on_abtdlg(self, widget):
   about = gtk.AboutDialog()
   about.set_program_name("PyGTK Dialog")
   about.set_version("0.1")
   about.set_authors("M.V.Lathkar")
   about.set_copyright("(c) Howcodex")
   about.set_comments("About Dialog example")
   about.set_website("http://www.howcodex.com")
   about.run()
   about.destroy()

The selected font is applied to the text of label placed on the toplevel window.

Select Font

The following is the output −

Font Dialog Boxes
Advertisements