Module albow.widgets.Label
Source code
import logging
from pygame import Surface
from albow.utils import overridable_property
from albow.core.ui.Widget import Widget
from albow.themes.ThemeProperty import ThemeProperty
class Label(Widget):
"""
Initializes the label with the given text and font. If a width is specified, it is used, otherwise the
label is initially made just wide enough to contain the text. The text may consist of more than one line,
separated by '\\n'. The initial height is determined by the number of lines and the font specified at
construction time or the default font from the theme.
"""
"""
Properties
"""
text = overridable_property('text')
"""
The text to be displayed. This can be changed dynamically
"""
align = overridable_property('align')
"""
Specifies the alignment of the text within the widget's rect. One of 'l', 'c' or 'r' for left, center or
right.
"""
highlight_color = ThemeProperty('highlight_color')
"""The color to use for highlighting the label"""
disabled_color = ThemeProperty('disabled_color')
"""The color to use when the label is disabled"""
highlight_bg_color = ThemeProperty('highlight_bg_color')
"""The highlight background color"""
enabled_bg_color = ThemeProperty('enabled_bg_color')
"""The enabled background color"""
disabled_bg_color = ThemeProperty('disabled_bg_color')
"""The disabled background color"""
enabled = True
"""Indicates if label should be enabled. Defaults to True"""
highlighted = False
"""
Indicates whether the label should be highlighted. Defaults to False. If set to true you MUST define
highlight_color
"""
_align = 'l'
def __init__(self, text, width=None, **kwds):
"""
Args:
text: The label text
width: The width of the label
**kwds: Additional key value pairs that affect the label
"""
self.logger = logging.getLogger(__name__)
super().__init__(**kwds)
self.size = self.computeSize(text, width)
self._text = text
self.logger.debug("Control size %s", self.size)
def get_text(self):
return self._text
def set_text(self, theNewText):
self._text = theNewText
self.size = self.computeSize(theNewText)
def get_align(self):
return self._align
def set_align(self, x):
self._align = x
def draw(self, surface: Surface):
"""
Args:
surface: The surface onto which to draw
"""
if not self.enabled:
fg = self.disabled_color
bg = self.disabled_bg_color
elif self.highlighted:
fg = self.highlight_color
bg = self.highlight_bg_color
else:
fg = self.fg_color
bg = self.enabled_bg_color
self.draw_with(surface, fg, bg)
def draw_with(self, surface: Surface, fg: tuple, bg: tuple = None):
"""
Args:
surface: The surface to drawn on
fg: The foreground color
bg: The background color
Returns:
"""
if bg:
r = surface.get_rect()
b = self.border_width
if b:
e = - 2 * b
r.inflate_ip(e, e)
surface.fill(bg, r)
m = self.margin
align = self.align
width = surface.get_width()
y = m
lines = self.text.split("\n")
font = self.font
dy = font.get_linesize()
for line in lines:
image = font.render(line, True, fg)
r = image.get_rect()
r.top = y
if align == 'l':
r.left = m
elif align == 'r':
r.right = width - m
else:
r.centerx = width // 2
surface.blit(image, r)
y += dy
def computeSize(self, theText: str, theWidth: int = None) -> tuple:
"""
Args:
theText: The text from which to compute the label size
theWidth: A *must* have minimum width
Returns: a tuple of the from (width, height)
"""
font = self.font
lines = theText.split("\n")
tw, th = 0, 0
for line in lines:
w, h = font.size(line)
tw = max(tw, w)
th += h
if theWidth is not None:
tw = theWidth
else:
tw = max(1, tw)
d = 2 * self.margin
adjustedWidth = tw + d
adjustedHeight = th + d
# self.size = (tw + d, th + d)
# Python 3 update
size = (adjustedWidth, adjustedHeight)
return size
Classes
class Label (text, width=None, **kwds)
-
Initializes the label with the given text and font. If a width is specified, it is used, otherwise the label is initially made just wide enough to contain the text. The text may consist of more than one line, separated by '\n'. The initial height is determined by the number of lines and the font specified at construction time or the default font from the theme.
Args
text
- The label text
width
- The width of the label
**kwds
- Additional key value pairs that affect the label
Source code
class Label(Widget): """ Initializes the label with the given text and font. If a width is specified, it is used, otherwise the label is initially made just wide enough to contain the text. The text may consist of more than one line, separated by '\\n'. The initial height is determined by the number of lines and the font specified at construction time or the default font from the theme. """ """ Properties """ text = overridable_property('text') """ The text to be displayed. This can be changed dynamically """ align = overridable_property('align') """ Specifies the alignment of the text within the widget's rect. One of 'l', 'c' or 'r' for left, center or right. """ highlight_color = ThemeProperty('highlight_color') """The color to use for highlighting the label""" disabled_color = ThemeProperty('disabled_color') """The color to use when the label is disabled""" highlight_bg_color = ThemeProperty('highlight_bg_color') """The highlight background color""" enabled_bg_color = ThemeProperty('enabled_bg_color') """The enabled background color""" disabled_bg_color = ThemeProperty('disabled_bg_color') """The disabled background color""" enabled = True """Indicates if label should be enabled. Defaults to True""" highlighted = False """ Indicates whether the label should be highlighted. Defaults to False. If set to true you MUST define highlight_color """ _align = 'l' def __init__(self, text, width=None, **kwds): """ Args: text: The label text width: The width of the label **kwds: Additional key value pairs that affect the label """ self.logger = logging.getLogger(__name__) super().__init__(**kwds) self.size = self.computeSize(text, width) self._text = text self.logger.debug("Control size %s", self.size) def get_text(self): return self._text def set_text(self, theNewText): self._text = theNewText self.size = self.computeSize(theNewText) def get_align(self): return self._align def set_align(self, x): self._align = x def draw(self, surface: Surface): """ Args: surface: The surface onto which to draw """ if not self.enabled: fg = self.disabled_color bg = self.disabled_bg_color elif self.highlighted: fg = self.highlight_color bg = self.highlight_bg_color else: fg = self.fg_color bg = self.enabled_bg_color self.draw_with(surface, fg, bg) def draw_with(self, surface: Surface, fg: tuple, bg: tuple = None): """ Args: surface: The surface to drawn on fg: The foreground color bg: The background color Returns: """ if bg: r = surface.get_rect() b = self.border_width if b: e = - 2 * b r.inflate_ip(e, e) surface.fill(bg, r) m = self.margin align = self.align width = surface.get_width() y = m lines = self.text.split("\n") font = self.font dy = font.get_linesize() for line in lines: image = font.render(line, True, fg) r = image.get_rect() r.top = y if align == 'l': r.left = m elif align == 'r': r.right = width - m else: r.centerx = width // 2 surface.blit(image, r) y += dy def computeSize(self, theText: str, theWidth: int = None) -> tuple: """ Args: theText: The text from which to compute the label size theWidth: A *must* have minimum width Returns: a tuple of the from (width, height) """ font = self.font lines = theText.split("\n") tw, th = 0, 0 for line in lines: w, h = font.size(line) tw = max(tw, w) th += h if theWidth is not None: tw = theWidth else: tw = max(1, tw) d = 2 * self.margin adjustedWidth = tw + d adjustedHeight = th + d # self.size = (tw + d, th + d) # Python 3 update size = (adjustedWidth, adjustedHeight) return size
Ancestors
Subclasses
Class variables
var align
-
Specifies the alignment of the text within the widget's rect. One of 'l', 'c' or 'r' for left, center or right.
var disabled_bg_color
-
The disabled background color
var disabled_color
-
The color to use when the label is disabled
var enabled
-
Indicates if label should be enabled. Defaults to True
var enabled_bg_color
-
The enabled background color
var highlight_bg_color
-
The highlight background color
var highlight_color
-
The color to use for highlighting the label
var highlighted
-
Indicates whether the label should be highlighted. Defaults to False. If set to true you MUST define highlight_color
var text
-
The text to be displayed. This can be changed dynamically
Methods
def computeSize(self, theText, theWidth=None)
-
Args
theText
- The text from which to compute the label size
theWidth
- A must have minimum width
Returns
:a
tuple
ofthe
from
(width
,height
)
Source code
def computeSize(self, theText: str, theWidth: int = None) -> tuple: """ Args: theText: The text from which to compute the label size theWidth: A *must* have minimum width Returns: a tuple of the from (width, height) """ font = self.font lines = theText.split("\n") tw, th = 0, 0 for line in lines: w, h = font.size(line) tw = max(tw, w) th += h if theWidth is not None: tw = theWidth else: tw = max(1, tw) d = 2 * self.margin adjustedWidth = tw + d adjustedHeight = th + d # self.size = (tw + d, th + d) # Python 3 update size = (adjustedWidth, adjustedHeight) return size
def draw(self, surface)
-
Args
surface
- The surface onto which to draw
Source code
def draw(self, surface: Surface): """ Args: surface: The surface onto which to draw """ if not self.enabled: fg = self.disabled_color bg = self.disabled_bg_color elif self.highlighted: fg = self.highlight_color bg = self.highlight_bg_color else: fg = self.fg_color bg = self.enabled_bg_color self.draw_with(surface, fg, bg)
def draw_with(self, surface, fg, bg=None)
-
Args
surface
- The surface to drawn on
fg
-
The foreground color
bg
-
The background color
Returns:
Source code
def draw_with(self, surface: Surface, fg: tuple, bg: tuple = None): """ Args: surface: The surface to drawn on fg: The foreground color bg: The background color Returns: """ if bg: r = surface.get_rect() b = self.border_width if b: e = - 2 * b r.inflate_ip(e, e) surface.fill(bg, r) m = self.margin align = self.align width = surface.get_width() y = m lines = self.text.split("\n") font = self.font dy = font.get_linesize() for line in lines: image = font.render(line, True, fg) r = image.get_rect() r.top = y if align == 'l': r.left = m elif align == 'r': r.right = width - m else: r.centerx = width // 2 surface.blit(image, r) y += dy
def get_align(self)
-
Source code
def get_align(self): return self._align
def get_text(self)
-
Source code
def get_text(self): return self._text
def set_align(self, x)
-
Source code
def set_align(self, x): self._align = x
def set_text(self, theNewText)
-
Source code
def set_text(self, theNewText): self._text = theNewText self.size = self.computeSize(theNewText)
Inherited members
Widget
:add
add_anchor
add_centered
anchor
attention_lost
augment_mouse_event
bg_color
bg_image
border_color
border_width
call_handler
call_parent_handler
defer_drawing
dismiss
draw_over
fg_color
focus
focus_switch
font
get_cursor
get_focus
get_margin_rect
get_root
get_top_widget
get_visible
global_to_local
has_focus
inherited
invalidate
is_gl_container
key_down
key_up
local_to_global
margin
menu_bar
parent
parent_resized
present
rect
relative_mode
remove
remove_anchor
resized
scale_bg
sel_color
set_parent
set_size_for_text
tab_stop
visible