|
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" xmlns:e="http://projectmallard.org/experimental/" type="guide" style="task" id="label.py" xml:lang="es">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">Label (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#display-widgets"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="properties.py"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="strings.py"/>
|
|
Packit |
1470ea |
<link type="next" xref="properties.py"/>
|
|
Packit |
1470ea |
<revision version="0.2" date="2012-06-18" 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 |
<credit type="author">
|
|
Packit |
1470ea |
<name>Sebastian Pölsterl</name>
|
|
Packit |
1470ea |
<email its:translate="no">sebp@k-d-w.org</email>
|
|
Packit |
1470ea |
<years>2012</years>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<desc>Un widget que muestra una cantidad pequeña o mediana de texto</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Daniel Mustieles</mal:name>
|
|
Packit |
1470ea |
<mal:email>daniel.mustieles@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011 - 2017</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Nicolás Satragno</mal:name>
|
|
Packit |
1470ea |
<mal:email>nsatragno@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2012 - 2013</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Jorge González</mal:name>
|
|
Packit |
1470ea |
<mal:email>jorgegonz@svn.gnome.org</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>Etiqueta</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/label.png"/>
|
|
Packit |
1470ea |
Una etiqueta sencilla
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="code">
|
|
Packit |
1470ea |
<title>Código usado para generar este ejemplo</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
from gi.repository import Gtk
|
|
Packit |
1470ea |
import sys
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyWindow(Gtk.ApplicationWindow):
|
|
Packit |
1470ea |
# constructor for a Gtk.ApplicationWindow
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def __init__(self, app):
|
|
Packit |
1470ea |
Gtk.Window.__init__(self, title="Welcome to GNOME", application=app)
|
|
Packit |
1470ea |
self.set_default_size(200, 100)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# create a label
|
|
Packit |
1470ea |
label = Gtk.Label()
|
|
Packit |
1470ea |
# set the text of the label
|
|
Packit |
1470ea |
label.set_text("Hello GNOME!")
|
|
Packit |
1470ea |
# add the label to the window
|
|
Packit |
1470ea |
self.add(label)
|
|
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 |
|
|
Packit |
1470ea |
Otra manera de obtener lo que se tiene en el ejemplo es crear la etiqueta como una instancia de otra clase y añadirle la instancia de MyWindow en el método do_activate(self) :
|
|
Packit |
1470ea |
<note>
|
|
Packit |
1470ea |
Las líneas de texto resaltadas indican que el código es diferente al del fragmento anterior.
|
|
Packit |
1470ea |
</note>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a class to define a window
|
|
Packit |
1470ea |
class MyWindow(Gtk.ApplicationWindow):
|
|
Packit |
1470ea |
def __init__(self, app):
|
|
Packit |
1470ea |
Gtk.Window.__init__(self, title="Welcome to GNOME", application=app)
|
|
Packit |
1470ea |
self.set_default_size(200, 100)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a class to define a label
|
|
Packit |
1470ea |
<e:hi>
|
|
Packit |
1470ea |
class MyLabel(Gtk.Label):
|
|
Packit |
1470ea |
def __init__(self):
|
|
Packit |
1470ea |
Gtk.Label.__init__(self)
|
|
Packit |
1470ea |
self.set_text("Hello GNOME!")
|
|
Packit |
1470ea |
</e:hi>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyApplication(Gtk.Application):
|
|
Packit |
1470ea |
def __init__(self):
|
|
Packit |
1470ea |
Gtk.Application.__init__(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_activate(self):
|
|
Packit |
1470ea |
# create an instance of MyWindow
|
|
Packit |
1470ea |
win = MyWindow(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# create an instance of MyLabel
|
|
Packit |
1470ea |
<e:hi>
|
|
Packit |
1470ea |
label = MyLabel()
|
|
Packit |
1470ea |
</e:hi>
|
|
Packit |
1470ea |
# and add it to the window
|
|
Packit |
1470ea |
<e:hi>
|
|
Packit |
1470ea |
win.add(label)
|
|
Packit |
1470ea |
</e:hi>
|
|
Packit |
1470ea |
# show the window and everything on it
|
|
Packit |
1470ea |
win.show_all()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="methods">
|
|
Packit |
1470ea |
<title>Métodos útiles para un widget «Label»</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<note style="tip">
|
|
Packit |
1470ea |
Puede encontrar una explicación de cómo trabajar con cadenas en GTK+ en la <link xref="strings.py"/>.
|
|
Packit |
1470ea |
</note>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>set_line_wrap(True) rompe líneas si el texto de la etiqueta excede el tamaño del widget. </item>
|
|
Packit |
1470ea |
<item>set_justify(Gtk.Justification.LEFT) (o Gtk.Justification.RIGHT, Gtk.Justification.CENTER, Gtk.Justification.FILL ) establece la alineación de las líneas en el texto de la etiqueta respecto de sí. El método no tiene efecto en una etiqueta de una sola línea. </item>
|
|
Packit |
1470ea |
<item>Para decorar el texto se puede usar set_markup("texto") , donde "texto" es un texto en el <link href="http://developer.gnome.org/pango/stable/PangoMarkupFormat.html">lenguaje de marcación Pango</link>. Un ejemplo:
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
label.set_markup("Text can be <small>small</small>, <big>big</big>, "
|
|
Packit |
1470ea |
"<b>bold</b>, <i>italic</i> and even point to somewhere "
|
|
Packit |
1470ea |
"on the <a href=\"http://www.gtk.org\" "
|
|
Packit |
1470ea |
"title=\"Click to find out more\">internet</a>.")
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="references">
|
|
Packit |
1470ea |
<title>Referencias de la API</title>
|
|
Packit |
1470ea |
En este ejemplo se usa lo siguiente:
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkLabel.html">GtkLabel</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkWindow.html">GtkWindow</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|