Blame platform-demos/pt_BR/label.py.page

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="pt-BR">
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>A widget that displays a small to medium amount of text</desc>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Rafael Ferreira</mal:name>
Packit 1470ea
      <mal:email>rafael.f.f1@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2013</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>Label</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/label.png"/>
Packit 1470ea
  

A simple label

Packit 1470ea
Packit 1470ea
  <links type="section"/>
Packit 1470ea
Packit 1470ea
  <section id="code">
Packit 1470ea
  <title>Code used to generate this example</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
    

Another way to obtain what we have in the example is to create the label as an instance of another class and add it to the instance of MyWindow in the do_activate(self) method:

Packit 1470ea
    <note>
Packit 1470ea
      

The highlighted lines indicate code that is different from the previous snippet.

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>Useful methods for a Label widget</title>
Packit 1470ea
  
Packit 1470ea
  <note style="tip">
Packit 1470ea
    

An explanation of how to work with strings in GTK+ can be found in <link xref="strings.py"/>.

Packit 1470ea
  </note>
Packit 1470ea
Packit 1470ea
  <list>
Packit 1470ea
    <item>

set_line_wrap(True) breaks lines if the text of the label exceeds the size of the widget.

</item>
Packit 1470ea
    <item>

set_justify(Gtk.Justification.LEFT) (or Gtk.Justification.RIGHT, Gtk.Justification.CENTER, Gtk.Justification.FILL) sets the alignment of the lines in the text of the label relative to each other. The method has no effect on a single-line label.

</item>
Packit 1470ea
    <item>

For decorated text we can use set_markup("text"), where "text" is a text in the <link href="http://developer.gnome.org/pango/stable/PangoMarkupFormat.html">Pango Markup Language</link>. An example:

Packit 1470ea
      
Packit 1470ea
label.set_markup("Text can be <small>small</small>, <big>big</big>, "
Packit 1470ea
                 "bold, italic and even point to somewhere "
Packit 1470ea
                 "on the 
Packit 1470ea
                 "title=\"Click to find out more\">internet.")]]>
Packit 1470ea
    </item>
Packit 1470ea
  </list>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="references">
Packit 1470ea
  <title>API References</title>
Packit 1470ea
  

In this sample we used the following:

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>