Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Label in Python GTK+ 3
The Label widget in Python GTK+ 3 is a fundamental component for displaying text in GUI applications. Labels show non-editable text content like descriptions, instructions, or captions that help users understand your application's interface.
Understanding GTK+ 3 Labels
Labels are core building blocks of GUI applications in GTK+ 3. They display static text that users typically cannot modify directly. However, labels can be enhanced with Pango markup for styling, made selectable for copying text, and even respond to user interactions.
The Gtk.Label class provides all the functionality needed to create and customize labels in your applications.
Basic Label Creation
Here's how to create a simple label with basic text ?
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class LabelWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Basic GTK+ 3 Label")
self.set_border_width(10)
# Create a basic label
label = Gtk.Label("Hello, GTK+ 3!")
self.add(label)
win = LabelWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
This creates a window with a simple text label. The Gtk.Label() constructor accepts the text string to display.
Using Pango Markup for Styling
Labels support Pango markup for rich text formatting including bold, italic, colors, and different sizes ?
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class StyledLabelWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Styled GTK+ 3 Label")
self.set_border_width(10)
# Create a label with Pango markup
label = Gtk.Label()
markup_text = "<span size='large' color='blue'><b>Styled Text</b></span>\n" + \
"<i>This text is italic</i>\n" + \
"<span color='red'>This text is red</span>"
label.set_markup(markup_text)
self.add(label)
win = StyledLabelWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
The set_markup() method enables Pango markup formatting. Common tags include <b> for bold, <i> for italic, and <span> for colors and sizes.
Creating Selectable Labels
Make labels selectable so users can copy the text for use elsewhere ?
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class SelectableLabelWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Selectable GTK+ 3 Label")
self.set_border_width(10)
# Create a selectable label
label = Gtk.Label("Click and drag to select this text!")
label.set_selectable(True)
label.set_line_wrap(True) # Enable line wrapping
self.add(label)
win = SelectableLabelWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
The set_selectable(True) method allows users to select and copy the label text. The set_line_wrap(True) method enables automatic line wrapping for longer text.
Label Alignment and Positioning
Control how text is aligned and positioned within the label widget ?
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class AlignedLabelWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Aligned GTK+ 3 Labels")
self.set_border_width(10)
self.set_default_size(300, 200)
# Create a vertical box to hold multiple labels
vbox = Gtk.VBox(spacing=10)
# Left-aligned label
left_label = Gtk.Label("Left aligned text")
left_label.set_halign(Gtk.Align.START)
# Center-aligned label
center_label = Gtk.Label("Center aligned text")
center_label.set_halign(Gtk.Align.CENTER)
# Right-aligned label
right_label = Gtk.Label("Right aligned text")
right_label.set_halign(Gtk.Align.END)
vbox.pack_start(left_label, False, False, 0)
vbox.pack_start(center_label, False, False, 0)
vbox.pack_start(right_label, False, False, 0)
self.add(vbox)
win = AlignedLabelWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
Use set_halign() to control horizontal alignment with values like Gtk.Align.START, Gtk.Align.CENTER, and Gtk.Align.END.
Common Label Properties
| Method | Purpose | Example |
|---|---|---|
set_text() |
Set plain text | label.set_text("Hello") |
set_markup() |
Set formatted text | label.set_markup("<b>Bold</b>") |
set_selectable() |
Enable text selection | label.set_selectable(True) |
set_line_wrap() |
Enable line wrapping | label.set_line_wrap(True) |
set_halign() |
Set horizontal alignment | label.set_halign(Gtk.Align.CENTER) |
Conclusion
GTK+ 3 labels are versatile widgets for displaying text in GUI applications. They support rich formatting through Pango markup, can be made selectable for user interaction, and offer flexible alignment options. Master these basics to create professional-looking applications with clear, well-formatted text elements.
