Friday, May 3, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 5065  / 2 Years ago, fri, august 19, 2022, 6:54:02

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.



Thanks,



Ian


More From » application-development

 Answers
3

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



#!/usr/bin/python

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.window.add(main_vbox)

self.tview = Gtk.ScrolledWindow()
main_vbox.add(self.tview)

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

self.tview.add(self.textview)

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.show_all()
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()
self.textview.modify_font(font_description)

def main():
app = TextViewWindow()
Gtk.main()

if __name__ == "__main__":
main()


My demo:



demo



Built-in dialog:



picker


[#35867] Saturday, August 20, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
confiorc

Total Points: 42
Total Questions: 121
Total Answers: 123

Location: India
Member since Wed, Aug 26, 2020
4 Years ago
;