Labels are elements displaying a short amount of text. They are non-interactive widgets and usually are used to display needed information to the user.
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 # Frame with the states. frm_states = _create_vframe ("States") for i in xrange (len (states)): lbl = Label (states[i]) 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) table.set_align (0, 0, ALIGN_TOP) # 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) table.set_align (0, 1, ALIGN_TOP) # Frame with mnemonic support. frm_mnemonics = _create_vframe ("Mnemonics") strings = ("#Simple Mnemonic", "A ## is displayed using '####'", "M#ultiple M#nemonics #have no #effect") for i in xrange (len (strings)): lbl = Label (strings[i]) frm_mnemonics.add_child (lbl) table.add_child (0, 2, frm_mnemonics) table.set_align (0, 2, ALIGN_TOP) # Frame with multiline labels. frm_multiline = _create_vframe ("Multiline labels") strings = ("Single line", "First lines" + os.linesep + "Second line", "First line" + os.linesep + "Second line" + os.linesep + "Third Line", "Two lines with a" + os.linesep + "#mnemonic") for i in xrange (len (strings)): lbl = Label (strings[i]) lbl.multiline = True frm_multiline.add_child (lbl) table.add_child (1, 0, frm_multiline) table.set_align (1, 0, ALIGN_TOP) 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 23. Label example