How can I change the font size in GTK?

17,458

Solution 1

If you want to change font overall in your app(s), I'd leave this job to gtkrc (then becomes a google question, and "gtkrc font" query brings us to this ubuntu forums link which has the following snippet of the the gtkrc file):

style "font"
{
font_name = "Corbel 8"
}
widget_class "*" style "font"
gtk-font-name = "Corbel 8"

(replace the font with the one you/user need)

Then the user will get consistent experience and will be able to change the settings easily without need for them to poke in the code and without you needing to handle the overhead of maintaining your personal configuration-related code. I understand you can make this setting more specific if you have a more precise definition for the widget_class.

YMMV for different platforms, but AFAIK this file is always present at some location if GTK is being used, and allows to the user to be in charge of presentation details.

Solution 2

In C, you can do:

gtk_widget_modify_font(lbl, pango_font_description_from_string("Tahoma 5.4"));

In PyGTK, I believe it's something like:

pangoFont = pango.FontDescription("Tahoma 5.4")
lbl.modify_font(pangoFont)
Share:
17,458
Claudiu
Author by

Claudiu

Graduated from Brown University. E-mail: [email protected]

Updated on June 04, 2022

Comments

  • Claudiu
    Claudiu almost 2 years

    Is there an easy way to change the font size of text elements in GTK? Right now the best I can do is do set_markup on a label, with something silly like:

    lbl.set_markup("<span font_desc='Tahoma 5.4'>%s</span>" % text)
    

    This 1) requires me to set the font , 2) seems like a lot of overhead (having to parse the markup), and 3) would make it annoying to change the font size of buttons and such. Is there a better way?

  • Claudiu
    Claudiu over 14 years
    is it possible to use a custom gtkrc file for just my application? if so, how can i do that?
  • Andrew Y
    Andrew Y over 14 years
  • GTK 1.2.6 fanboy
    GTK 1.2.6 fanboy about 6 years
    In C this would create a memory leak, since a pango font description needs to be freed with pango_font_description_free () after use (this is not obvious from the documentation of pango_font_description_from_string () itself). Other than that, it is a possible solution.
  • LFMekz
    LFMekz over 4 years
    Link is dead dang
  • LFMekz
    LFMekz over 4 years
    Freed immediately after use?