Labels are the main method of placing non-editable text in windows, for instance to place a title next to a Gtk.Entry widget. You can specify the text in the constructor, or later with the Gtk.Label.set_text() or Gtk.Label.set_markup() methods.
The width of the label will be adjusted automatically. You can produce multi-line labels by putting line breaks (“\n”) in the label string.
Labels can be made selectable with Gtk.Label.set_selectable(). Selectable labels allow the user to copy the label contents to the clipboard. Only labels that contain useful-to-copy information — such as error messages — should be made selectable.
Gtk.Label support some simple formatting, for instance allowing you to make some text bold, colored, or larger. You can do this by providing a string to Gtk.Label.set_markup(), using the Pango Markup syntax . For instance, <b>bold text</b> and <s>strikethrough text</s>. In addition, Gtk.Label supports clickable hyperlinks. The markup for links is borrowed from HTML, using the a with href and title attributes. GTK+ renders links similar to the way they appear in web browsers, with colored, underlined text. The title attribute is displayed as a tooltip on the link.
label.set_markup("Go to <a href=\"http://www.gtk.org\" title=\"Our website\">GTK+ website</a> for more")
Labels may contain mnemonics. Mnemonics are underlined characters in the label, used for keyboard navigation. Mnemonics are created by providing a string with an underscore before the mnemonic character, such as “_File”, to the functions Gtk.Label.new_with_mnemonic() or Gtk.Label.set_text_with_mnemonic(). Mnemonics automatically activate any activatable widget the label is inside, such as a Gtk.Button; if the label is not inside the mnemonic’s target widget, you have to tell the label about the target using Gtk.Label.set_mnemonic_widget().
Creates a new label with the given text inside it. If text is omitted, an empty label is created.
Creates a new label with text inside it.
If characters in text are preceded by an underscore, they are underlined. If you need a literal underscore character in a label, use ‘__’ (two underscores). The first underlined character represents a keyboard accelerator called a mnemonic. The mnemonic key can be used to activate another widget, chosen automatically, or explicitly using Gtk.Label.set_mnemonic_widget().
If Gtk.Label.set_mnemonic_widget() is not called, then the first activatable ancestor of the Gtk.Label will be chosen as the mnemonic widget. For instance, if the label is inside a button or menu item, the button or menu item will automatically become the mnemonic widget and be activated by the mnemonic.
Sets the alignment of the lines in the text of the label relative to each other. justification can be one of Gtk.Justification.LEFT, Gtk.Justification.RIGHT, Gtk.Justification.CENTER, Gtk.Justification.FILL. This method has no effect on labels containing only a single line.
If wrap is True, lines will be broken if text exceeds the widget’s size. If wrap is False, text will be cut off by the edge of the widget if it exceeds the widget size.
Parses markup which is marked up with the Pango text markup language , setting the label’s text accordingly. The markup passed must be valid; for example literal <, >, & characters must be escaped as < > and &.
If the label has been set so that it has an mnemonic key, the label can be associated with a widget that is the target of the mnemonic.
Selectable labels allow the user to select text from the label, for copy-and-paste.
Sets the text within this widget. It overwrites any text that was there before.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
from gi.repository import Gtk class LabelWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="Label Example") hbox = Gtk.Box(spacing=10) hbox.set_homogeneous(False) vbox_left = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10) vbox_left.set_homogeneous(False) vbox_right = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10) vbox_right.set_homogeneous(False) hbox.pack_start(vbox_left, True, True, 0) hbox.pack_start(vbox_right, True, True, 0) label = Gtk.Label("This is a normal label") vbox_left.pack_start(label, True, True, 0) label = Gtk.Label() label.set_text("This is a left-justified label.\nWith multiple lines.") label.set_justify(Gtk.Justification.LEFT) vbox_left.pack_start(label, True, True, 0) label = Gtk.Label("This is a right-justified label.\nWith multiple lines.") label.set_justify(Gtk.Justification.RIGHT) vbox_left.pack_start(label, True, True, 0) label = Gtk.Label("This is an example of a line-wrapped label. It " "should not be taking up the entire " "width allocated to it, but automatically " "wraps the words to fit.\n" " It supports multiple paragraphs correctly, " "and correctly adds " "many extra spaces. ") label.set_line_wrap(True) vbox_right.pack_start(label, True, True, 0) label = Gtk.Label("This is an example of a line-wrapped, filled label. " "It should be taking " "up the entire width allocated to it. " "Here is a sentence to prove " "my point. Here is another sentence. " "Here comes the sun, do de do de do.\n" " This is a new paragraph.\n" " This is another newer, longer, better " "paragraph. It is coming to an end, " "unfortunately.") label.set_line_wrap(True) label.set_justify(Gtk.Justification.FILL) vbox_right.pack_start(label, True, True, 0) label = Gtk.Label() 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>.") label.set_line_wrap(True) vbox_left.pack_start(label, True, True, 0) label = Gtk.Label.new_with_mnemonic("_Press Alt + P to select button to the right") vbox_left.pack_start(label, True, True, 0) label.set_selectable(True) button = Gtk.Button(label="Click at your own risk") label.set_mnemonic_widget(button) vbox_right.pack_start(button, True, True, 0) self.add(hbox) window = LabelWindow() window.connect("delete-event", Gtk.main_quit) window.show_all() Gtk.main()
|||(1, 2) Pango Markup Syntax, http://developer.gnome.org/pango/stable/PangoMarkupFormat.html|