Un FontChooserWidget con una función de retorno de llamada.
from gi.repository import Gtk
import sys
class MyWindow(Gtk.ApplicationWindow):
def __init__(self, app):
Gtk.Window.__init__(self, title="FontChooserWidget", application=app)
# a font chooser
self.font_chooser = Gtk.FontChooserWidget()
# a default font
self.font_chooser.set_font("Sans")
# a text to preview the font
self.font_chooser.set_preview_text(
"This is an example of preview text!")
# connect signal from the font chooser to the callback function
self.font_chooser.connect("notify::font", self.font_cb)
# add the font chooser to the window
self.add(self.font_chooser)
# callback function:
def font_cb(self, event, user_data):
# print in the terminal
print("You chose the font " + self.font_chooser.get_font())
class MyApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_activate(self):
win = MyWindow(self)
win.show_all()
def do_startup(self):
Gtk.Application.do_startup(self)
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)
En la línea 16, la señal «notify::font»
del widget se conecta a la función de retorno de llamada font_cb()
usando widget.connect(señal, función de retorno de llamada)
. Consulte la para obtener una explicación más detallada.
Para establecer la tipografía seleccionada inicialmente, use set_font(tipografía)
(donde tipografía
es el nombre de la tipografía) o set_font_desc(tipografía)
(donde tipografía
es la PangoFontDescription).
Para obtener la fuente seleccionada use get_font()
o get_font_desc()
.
Para cambiar el texto que se muestra en el área de previsualización, use set_preview_text()
.
En este ejemplo se usa lo siguiente:
GtkFontChooserWidget