Label (Python) Marta Maria Casetti mmcasetti@gmail.com 2012 Sebastian Pölsterl sebp@k-d-w.org 2012 Un élément graphique qui affiche une petite à moyenne quantité de texte Luc Rebert, traduc@rebert.name 2011 Alain Lojewski, allomervan@gmail.com 2011-2012 Luc Pionchon pionchon.luc@gmail.com 2011 Bruno Brouard annoa.b@gmail.com 2011-12 Luis Menina liberforce@freeside.fr 2014 Étiquette

Une étiquette simple

Code utilisé pour générer cet exemple from gi.repository import Gtk import sys class MyWindow(Gtk.ApplicationWindow): # constructor for a Gtk.ApplicationWindow def __init__(self, app): Gtk.Window.__init__(self, title="Welcome to GNOME", application=app) self.set_default_size(200, 100) # create a label label = Gtk.Label() # set the text of the label label.set_text("Hello GNOME!") # add the label to the window self.add(label) 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)

Une autre manière d'obtenir ce que contient cet exemple est de créer l'étiquette comme étant un exemple d'une autre classe et de l'ajouter à MyWindow dans la méthode do_activate(self) :

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

# 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()
Méthodes utiles pour un élément graphique étiquette

An explanation of how to work with strings in GTK+ can be found in .

La fonction set_line_wrap(True) renvoie le texte de l'étiquette à la ligne s'il excède la largeur de l'élément graphique.

La fonction set_justify(Gtk.Justification.LEFT) (ou Gtk.Justification.RIGHT, Gtk.Justification.CENTER, Gtk.Justification.FILL) définit l'alignement des lignes du texte de l'étiquette à une position relative les unes par rapport aux autres. La méthode n'a aucun effet sur une étiquette comportant une seule ligne.

Pour décorer le texte, nous pouvons utiliser set_markup("text"), où "text" est mis en langage Markup pango. Voici un exemple :

small, big, " "bold, italic and even point to somewhere " "on the internet.")]]>
Références API

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

GtkLabel

GtkWindow