Create  Edit  FrontPage  Index  Search  Changes  History  RSS  Login


Font Button

Gtk::FontButton is another type of specialized button that allows you to select font parameters for fonts which are installed on your system. Like most of the buttons this button is derived from Gtk::Button, however it emits a special signal when clicked, namely the signal "font-set". Let's look again at the Gtk::Button hierarchy:

|   Gtk::ToggleButton
|   |   Gtk::CheckButton
|   |   |   Gtk::RadioButton
|   Gtk::ColorButton
|   Gtk::FontButton
|   Gtk::LinkButton
|   Gtk::OptionMenu
|   Gtk::ScaleButton
|   |   Gtk::VolumeButton

There are two Font Button gadgets. The first is the Gtk::FontButton shown in the above Button hierarchy tree also seen on the first figure below on the right, and the second is the Gtk::FontSelectionDialog which opens after you click the Gtk::FontButton. You can see an example of such a dialogue below (the second figure on the left). However, you are not restricted to the use of either the Font Button or the Font Dialogue widgets to manipulate fonts, but they are a good starting point to learn the initial steps. The Font Dialogue widget displays the font options from which you can choose and immediately test your settings. These options include:

  • font name, a.k.a Family
  • font style
  • size

Font button widgets are initialized with:

font_button =


The "font" parameter is provided as a string in the format suggested in the above list: Family Style Size in this order. Each of the parameters is optional, and the default fot Gtk::FontButton is Sans 12, with no style parameter.

"Family" refers to the formal font name such as "Sans", "Sherif" or "Arial". Style options can vary between fonts, and normally include "Italic", "Bold", "Bold Italic" and "Regular". The latter, namely the "Regular" indicates no style. The size is the point size such as "8", "9", "10", etc.

Let's look at the example program now:


#!/usr/bin/env ruby

require 'gtk2'

def font_changed(f_btt, label)
  font = f_btt.font_name
  desc =
  label.text = "Font: %s" % [desc]

window =
window.border_width = 10
window.set_size_request(200, -1)
window.title = "Font Button"

window.signal_connect('delete_event') { false }
window.signal_connect('destroy') { Gtk.main_quit }

label ="Look at the font")
initial_font ="Sans Bold 12")

button =
button.title = "Choose a Font"

button.signal_connect('font_set') { |w| font_changed(w, label) }

vbox =, 5)


Font Manipulation Methods

You should be able to to modify font used in most any widget with the Gtk::Widget#modify_font(font), providing font is an instance of Pango::FontDescription. dialog-warning.pngHowever currently (Ruby 1.8.6 and Ruby-GNOME2 rel 2-0.17.0-rc1) this does not seem to be the case. I was only able to manipulate fonts on Gtk::Label objects and editable objects like Gtk::Entry. I could not change font on button text or button labels. I believe this will change in the future. Those of you who wish to revisit the example programs where font is manipulated and where I discovered the font problem I suggest you visit the session Fixed Containers and read the three short program listings (including comments). = nil)
Creates a new font description from (str) - a string representation in the form "[FAMILY-LIST] [STYLE-OPTIONS] [SIZE]", where:
  • FAMILY-LIST is a comma separated list of families optionally terminated by a comma,
  • STYLE_OPTIONS is a whitespace separated list of words where each WORD describes one of style, variant, weight, or stretch, and
  • SIZE is an decimal number (size in points). Any one of the options may be absent.

If FAMILY-LIST is absent, then the family_name field of the resulting font description will be initialized to nil. If STYLE-OPTIONS is missing, then all style options will be set to the default values. If SIZE is missing, the size in the resulting font description will be set to 0. If str is nil, creates a new font description structure with all fields unset.

This method returns a new Pango::FontDescription.

Sets the font to use for a widget to "font_desc" (the Pango::FontDescription to use). All other style values are left untouched. See also Gtk::Widget#modify_style.

Let's look at a short code snippet:

# font ="Monospace Bold 10")
# font ="Times,  Italic 20")
# font ="Comic Sans MS, Bold Italic 20")

font ="Nimbus Mono L, Bold Italic, 12")
Last modified:2012/09/15 05:26:35
References:[tut-gtk] [tut-gtk2-dialog-fontcolor] [tut-gtk2-txtw-textview] [tut-gtk2-btt] [tut-gtk2-selchoose]