|
Packit |
1470ea |
|
|
Packit |
1470ea |
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="fontchooserwidget.py" xml:lang="gl">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">FontChooserWidget (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#font-selectors"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="signals-callbacks.py"/>
|
|
Packit |
1470ea |
<link type="next" xref="filechooserdialog.py"/>
|
|
Packit |
1470ea |
<revision version="0.2" date="2012-05-05" status="draft"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<credit type="author copyright">
|
|
Packit |
1470ea |
<name>Marta Maria Casetti</name>
|
|
Packit |
1470ea |
<email its:translate="no">mmcasetti@gmail.com</email>
|
|
Packit |
1470ea |
<years>2012</years>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<desc>A widget to choose a font</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Fran Dieguez</mal:name>
|
|
Packit |
1470ea |
<mal:email>frandieguez@gnome.org</mal:email>
|
|
Packit |
1470ea |
<mal:years>2012-2013.</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>FontChooserWidget</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/fontchooserwidget.png"/>
|
|
Packit |
1470ea |
A FontChooserWidget with a callback function.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="code">
|
|
Packit |
1470ea |
<title>Código usado para xerar este exemplo</title>
|
|
Packit |
1470ea |
from gi.repository import Gtk
|
|
Packit |
1470ea |
import sys
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyWindow(Gtk.ApplicationWindow):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def __init__(self, app):
|
|
Packit |
1470ea |
Gtk.Window.__init__(self, title="FontChooserWidget", application=app)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a font chooser
|
|
Packit |
1470ea |
self.font_chooser = Gtk.FontChooserWidget()
|
|
Packit |
1470ea |
# a default font
|
|
Packit |
1470ea |
self.font_chooser.set_font("Sans")
|
|
Packit |
1470ea |
# a text to preview the font
|
|
Packit |
1470ea |
self.font_chooser.set_preview_text(
|
|
Packit |
1470ea |
"This is an example of preview text!")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# connect signal from the font chooser to the callback function
|
|
Packit |
1470ea |
self.font_chooser.connect("notify::font", self.font_cb)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# add the font chooser to the window
|
|
Packit |
1470ea |
self.add(self.font_chooser)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function:
|
|
Packit |
1470ea |
def font_cb(self, event, user_data):
|
|
Packit |
1470ea |
# print in the terminal
|
|
Packit |
1470ea |
print("You chose the font " + self.font_chooser.get_font())
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyApplication(Gtk.Application):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def __init__(self):
|
|
Packit |
1470ea |
Gtk.Application.__init__(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_activate(self):
|
|
Packit |
1470ea |
win = MyWindow(self)
|
|
Packit |
1470ea |
win.show_all()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_startup(self):
|
|
Packit |
1470ea |
Gtk.Application.do_startup(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
app = MyApplication()
|
|
Packit |
1470ea |
exit_status = app.run(sys.argv)
|
|
Packit |
1470ea |
sys.exit(exit_status)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
<section id="methods">
|
|
Packit |
1470ea |
<title>Useful methods for a FontChooserWidget</title>
|
|
Packit |
1470ea |
In line 16 the "notify::font" signal from the widget is connected to the callback function font_cb() using widget.connect(signal, callback function) . See <link xref="signals-callbacks.py"/> for a more detailed explanation.
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>To set the font which is initially selected, use set_font(font) (where font is the font name) or set_font_desc(font) (where font is the PangoFontDescription). </item>
|
|
Packit |
1470ea |
<item>To get the selected font use get_font() or get_font_desc() . </item>
|
|
Packit |
1470ea |
<item>To change the text which is shown in the preview area, use set_preview_text() . </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
<section id="references">
|
|
Packit |
1470ea |
<title>API References</title>
|
|
Packit |
1470ea |
Neste exemplo empregaremos o seguinte:
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkFontChooserWidget.html">GtkFontChooserWidget</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|