How to get the default font used by the system using pygtk or pango?

3.9k views Asked by At

I have a fontbutton using pygtk. Initially, i do not want to set the font, as the system will take its default one. My question is what is the line of code to get the default font used by the system , so that i keep things default at first. Later after user changes the font, their respective fonts should apply. Can anyone help?

4

There are 4 answers

2
xubuntix On

Gtk is a toolkit that can run on many operating systems and desktop environments. Gtk is not responsible for the default fonts and colors. Therefore there is (as far as I know) no single function to get these values from Gtk.

There is probably not one default font in your OS/DE, but many. On Ubuntu for example, you can choose those in the (advanced) system settings:

enter image description here

If you need to know the default font of a specific widget, e.g. a Label, you can do the following:

from gi.repository import Gtk
l = Gtk.Label("Hello")
ls = l.get_style()
ls.font_desc.to_string()
# Ubuntu 11
0
ptomato On

If you are on GNOME, then you can get the default font like this. (Caution: untested)

from gi.repository import Gio
settings = Gio.Settings('org.gnome.desktop.interface')
font_name = settings.get_string('font-name')

You can also use the keys monospace-font-name and document-font-name as appropriate (I forget the name of the key for the window title font.)

0
trin94 On

On PyGObject this works for the default font:

settings = Gtk.Settings.get_default()
font_name = settings.get_property('gtk-font-name')

print(font_name)
# 'Cantarell 11' 

Unfortunatelly I have not found a way accessing the monospace font or any other font.

0
user2963623 On

Kind of late, but if anyone faces this in the future, a neat way would be to use the font module of pygame. Of course this would require you to install and import pygame and initialize it!

import pygame
pygame.font.init()
def_font = pygame.font.get_default_font()