简体   繁体   中英

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

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?

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

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()

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:

在此处输入图片说明

If you need to know the default font of a specific widget, eg 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM