Labels are non-interactive decorative user interface elements, which provide certain information to the user.
The Label
class can display a short amount
of text and allows you to control interaction with other widgets
using keyboard mnemonics.
To create a Label
, you typically will use
my_label = Label (text)
To set the text after creation, use the text
attribute or set_text()
method.
label.text = "New Text" label.set_text ("New Text")
It is possible to have multiline text by setting the multiline attribute to True:
label.multiline = True label.set_multiline (True)
Labels support keyboard accelerators, so called mnemonics, which
can activate other widgets. The '#' will cause the directly
following to work as mnemonic character. If you want to cause
the Label
to display a normal '#', use
'##' in the text.
label.text = '#Mnemonic' label.set_text ('A simple hash: ##')
If a mnemonic is set up, you usually have to set the widget, which should be activated by the mnemonic as well.
label.widget = another_widget
Below you will find an example to illustrate most of the
abilities of the Label
widget class. You
do not need to care about other widgets like the
Frame
class for now as those are
explained later on.
You can find the following example as a python script under
examples/label.py
.
# Label examples. import os from ocempgui.widgets import * from ocempgui.widgets.Constants import * def _create_vframe (text): frame = VFrame (Label (text)) frame.spacing = 5 frame.align = ALIGN_LEFT return frame def create_label_view (): states = ("STATE_NORMAL", "STATE_ENTERED", "STATE_ACTIVE", "STATE_INSENSITIVE") table = Table (2, 3) table.spacing = 5 table.set_row_align (0, ALIGN_TOP) table.set_row_align (1, ALIGN_TOP) # Frame with the states. frm_states = _create_vframe ("States") for i, s in enumerate (states): lbl = Label (s) if STATE_TYPES[i] == STATE_INSENSITIVE: lbl.sensitive = False else: lbl.state = STATE_TYPES[i] frm_states.add_child (lbl) table.add_child (0, 0, frm_states) # Frame with different padding. frm_padding = _create_vframe ("Padding") for i in xrange (5): lbl = Label ("Padding: %dpx" % (i * 2)) lbl.padding = i * 2 frm_padding.add_child (lbl) table.add_child (0, 1, frm_padding) # Frame with mnemonic support. frm_mnemonics = _create_vframe ("Mnemonics") strings = ("#Simple Mnemonic", "A ## is displayed using '####'", "M#ultiple M#nemonics #have no #effect") for s in strings: lbl = Label (s) frm_mnemonics.add_child (lbl) table.add_child (0, 2, frm_mnemonics) # Frame with multiline labels. frm_multiline = _create_vframe ("Multiline labels") frm_multiline.align = ALIGN_NONE strings = ("Single line", "First lines\nSecond line", "First line\nSecond line\nThird Line", "Two lines with a\n#mnemonic") for s in strings: lbl = Label (s) lbl.multiline = True frm_multiline.add_child (lbl) table.add_child (1, 0, frm_multiline) return table if __name__ == "__main__": # Initialize the drawing window. re = Renderer () re.create_screen (500, 350) re.title = "Label examples" re.color = (234, 228, 223) re.add_widget (create_label_view ()) # Start the main rendering loop. re.start ()
Example 26. Label example
The ImageLabel
is a decorative widget
holding an image and not much functionality besides that. It
does not support mnemonics nor text and is mainly used to
display images in a GUI, whenever they should fit smoothly into
the layout.
To create an ImageLabel
, you have pass
either the name of a file to load (including the full path to
it) or a pygame.Surface
object to
display.
imagelabel = ImageLabel ("path/to/an/image.png") imagelabel = ImageLabel (pygame_surface)
set_picture()
method.
In contrast to the Label
class, the
ImageLabel
supports different border
styles to adjust its look and feel without the need to override
its drawing methods.
imagelabel.border = BORDER_NONE imagelabel.set_border (BORDER_NONE)
Below you will find an example to illustrate most of the
abilities of the ImageLabel
widget
class. You do not need to care about other widgets like the
Frame
class for now as those are
explained later on.
You can find the following example as a python script under
examples/imagelabel.py
.
# ImageLabel examples. import os from ocempgui.draw import Image from ocempgui.widgets import * from ocempgui.widgets.Constants import * def _create_vframe (text): frame = VFrame (Label (text)) frame.spacing = 5 frame.align = ALIGN_LEFT return frame def create_imagelabel_view (): image = Image.load_image ("./image.png") table = Table (1, 3) table.spacing = 5 table.set_row_align (0, ALIGN_TOP) # Frame with the states. frm_states = _create_vframe ("States") for i, s in enumerate (STATE_TYPES): lbl = ImageLabel (image) if s == STATE_INSENSITIVE: lbl.sensitive = False else: lbl.state = s frm_states.add_child (lbl) table.add_child (0, 0, frm_states) # Frame with different padding. frm_padding = _create_vframe ("Padding") for i in xrange (5): lbl = ImageLabel (image) lbl.border = BORDER_FLAT lbl.padding = i * 2 frm_padding.add_child (lbl) table.add_child (0, 1, frm_padding) # Borders. frm_borders = _create_vframe ("Borders") for border in BORDER_TYPES: lbl = ImageLabel (image) lbl.border = border frm_borders.add_child (lbl) table.add_child (0, 2, frm_borders) return table if __name__ == "__main__": # Initialize the drawing window. re = Renderer () re.create_screen (500, 350) re.title = "ImageLabel examples" re.color = (234, 228, 223) re.add_widget (create_imagelabel_view ()) # Start the main rendering loop. re.start ()
Example 27. ImageLabel example