Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In an app using PyGI and GTK3, I'm trying to let the user set the font family and size in a TextView through my app's settings dialog. In PyGI the TextView object has "set" methods for several properties, but not for font properties. I can set the font family via CSS using a CSS style provider. But I can't change that CSS dynamically to respect the font selected by a user. So how can I do this? (Note that the widget in question is actually the TextEditor from the quickly-widgets package and I'm using Quickly to build the app.)

Edit to clarify: I'm not wedded to using CSS, that just seems to be GTK's preferred approach. What I'm hoping for is a method of some kind, like GtkTextView.setFontProperties() or something like that. I can't find anything like that for font properties.



share|improve this question
up vote 3 down vote accepted

You can set the font in a textview using GTK+'s built-in button and dialog for this, Gtk.FontButton():


from gi.repository import Gtk

class TextViewWindow:
    def __init__(self):
        self.window = Gtk.Window()
        self.window.set_default_size(400, 400)

        main_vbox = Gtk.VBox(homogeneous=False, spacing=0)

        self.tview = Gtk.ScrolledWindow()

        self.textview = Gtk.TextView()
        self.textbuffer = self.textview.get_buffer()
        self.textbuffer.set_text("Here is a text view.")


        self.font_button = Gtk.FontButton()
        self.font_button.connect('font-set', self.on_font_set)
        main_vbox.pack_start(self.font_button, False, False, 0)

        self.window.connect('destroy', lambda w: Gtk.main_quit())

    def on_font_set(self, widget):
        font_description = widget.get_font_desc()
        print "You chose: " + widget.get_font()

def main():
    app = TextViewWindow()

if __name__ == "__main__":

My demo:


Built-in dialog:


share|improve this answer
Interesting... Is it necessary to import Pango though? I don't see you explicitly call anything from it. Also that's a really horrid font selection dialog... – Ian B. Aug 31 '12 at 23:44
I started writing that before I decided to actually throw the demo together. Pango isn't actually used. Fixed above. – andrewsomething Sep 1 '12 at 1:17
That's great. Thanks (and sorry for the delayed accept). – monotasker Sep 27 '12 at 20:26
Just a note, modify_font is deprecated now in favor of override_font for Gtk. – RobotHumans Jul 11 '14 at 14:00

You should be able to use texttags to do this, but I'm not sure it's the "best" way.

First you would select all the text, here's the page from the readthedocs site on applying tags.

And here's a list of properties from the C/C++ reference.

Just adding this on... I recently needed to do something similar, and I had to do a little looking to come up with this snippet:

self.textview = Gtk.TextView()
    Pango.font_description_from_string('DejaVu Sans Mono 12')
share|improve this answer
Thanks @aking1012. But I am really looking for something lower-level. If the CSS style providers are now supposed to be the standard way of styling GTK widgets, it seems to me that there should still be a way of setting that style information dynamically. – monotasker Aug 29 '12 at 16:35
That's fair, but you want to: a)set dynamically and b)use css. Well, setting dynamically involves a function call anyway and making a load of CSS files to switch between seems counter-intuitive to me. I would set it either a)once in the glade file or b)through function calls. – RobotHumans Aug 29 '12 at 17:31
Yeah, @aking1012, I see what you mean. It's just that the GTK3 api (and PyGI) provides methods to set most style features globally for a widget object, even if you have defaults set in your CSS provider. It seems very odd to me that there isn't something similar for font properties -- so I'm hoping that I've just missed something. Tags are meant, I think, for working with portions of text, not for setting global styles on the widget. – monotasker Aug 30 '12 at 13:26
I see your point as well. – RobotHumans Aug 30 '12 at 13:28
I don't know exactly how to solve your problem, but there's GtkFontChooserWidget Maybe you can use it somehow. – mivoligo Aug 30 '12 at 20:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.