# Label (Python)

Marta Maria Casetti
mmcasetti@gmail.com 2012
Sebastian Pölsterl
sebp@k-d-w.org 2012
A widget that displays a small to medium amount of text

# Étiquette

Une étiquette simple

# Code used to generate this example

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:

# a class to define a window class MyWindow(Gtk.ApplicationWindow): def __init__(self, app): Gtk.Window.__init__(self, title="Welcome to GNOME", application=app) self.set_default_size(200, 100)

# a class to define a label class MyLabel(Gtk.Label): def __init__(self): Gtk.Label.__init__(self) self.set_text("Hello GNOME!")

class MyApplication(Gtk.Application): def __init__(self): Gtk.Application.__init__(self)

def do_activate(self): # create an instance of MyWindow win = MyWindow(self) # create an instance of MyLabel label = MyLabel() # and add it to the window win.add(label) # show the window and everything on it win.show_all()

# Useful methods for a Label widget

An explanation of how to deal with strings and Unicode (and a recap of what these things are) can be found here.

`label = Gtk.Label(label="Hello GNOME!")` could also be used create a label and set the text directly. For a general discussion of this, see here.

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

`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.

For decorated text we can use `set_markup("text")`, where `"text"` is a text in the Pango Markup Language. An example:

label.set_markup("Text can be <small>small</small>, <big>big</big>, " "<b>bold</b>, <i>italic</i> and even point to somewhere " "in the <a href=\"http://www.gtk.org\" " "title=\"Click to find out more\">internets</a>.")

# API References

Dans cet exemple, les éléments suivants sont utilisés :

GtkLabel

GtkWindow