Module albow.choices.TextMultiChoice
Source code
from pygame import draw
from pygame import Surface
from pygame import Rect
from albow.utils import blit_in_rect
from albow.choices.MultiChoice import MultiChoice
class TextMultiChoice(MultiChoice):
"""
TextMultichoice is a Multichoice control that displays its values in the form of text.
.. Note::
In addition to the highlight styles defined by PaletteView,
TextMultichoice also provides 'arrows', which highlights the selected value with a pair of
arrowheads above and below.
"""
def __init__(self, values, labels=None, **kwds):
"""
Initializes the control with the given values and corresponding labels. If no labels are specified,
they are derived by applying str() to the values
Args:
values: The values
labels: The displayed associated labels
**kwds:
"""
if not labels:
labels = map(str, values)
font = self.predict_font(kwds)
# d = 2 * self.predict(kwds, 'margin')
cd = 2 * self.predict(kwds, 'cell_margin')
wmax = 0
hmax = 0
for (w, h) in map(font.size, labels):
wmax = max(wmax, w)
hmax = max(hmax, h)
cw = wmax + cd
ch = hmax + cd
super().__init__((cw, ch), values, **kwds)
self.labels = labels
def draw_item(self, theSurface: Surface, n: int, theRect: Rect):
buf = self.font.render(self.labels[n], True, self.fg_color)
blit_in_rect(theSurface, buf, theRect, self.align, self.margin)
def draw_prehighlight(self, theSurface: Surface, theItemNumber: int, theRect: Rect):
if self.highlight_style == 'arrows':
self.draw_arrows(theSurface, theRect)
else:
MultiChoice.draw_prehighlight(self, theSurface, theItemNumber, theRect)
def draw_arrows(self, theSurface: Surface, theRect: Rect):
"""
Args:
theSurface: pygame surface to drawn
theRect: The pygame rectangle to draw in
"""
m = self.margin
color = self.sel_color or self.fg_color
x, y = theRect.midtop
pts = [(x - m, y - m), (x + m, y - m), (x, y)]
draw.polygon(theSurface, color, pts)
x, y = theRect.midbottom
y -= 1
pts = [(x - m, y + m), (x + m, y + m), (x, y)]
draw.polygon(theSurface, color, pts)
Classes
class TextMultiChoice (values, labels=None, **kwds)
-
TextMultichoice is a Multichoice control that displays its values in the form of text.
Note
In addition to the highlight styles defined by PaletteView, TextMultichoice also provides 'arrows', which highlights the selected value with a pair of arrowheads above and below.
Initializes the control with the given values and corresponding labels. If no labels are specified, they are derived by applying str() to the values
Args
values
- The values
labels
- The displayed associated labels
**kwds:
Source code
class TextMultiChoice(MultiChoice): """ TextMultichoice is a Multichoice control that displays its values in the form of text. .. Note:: In addition to the highlight styles defined by PaletteView, TextMultichoice also provides 'arrows', which highlights the selected value with a pair of arrowheads above and below. """ def __init__(self, values, labels=None, **kwds): """ Initializes the control with the given values and corresponding labels. If no labels are specified, they are derived by applying str() to the values Args: values: The values labels: The displayed associated labels **kwds: """ if not labels: labels = map(str, values) font = self.predict_font(kwds) # d = 2 * self.predict(kwds, 'margin') cd = 2 * self.predict(kwds, 'cell_margin') wmax = 0 hmax = 0 for (w, h) in map(font.size, labels): wmax = max(wmax, w) hmax = max(hmax, h) cw = wmax + cd ch = hmax + cd super().__init__((cw, ch), values, **kwds) self.labels = labels def draw_item(self, theSurface: Surface, n: int, theRect: Rect): buf = self.font.render(self.labels[n], True, self.fg_color) blit_in_rect(theSurface, buf, theRect, self.align, self.margin) def draw_prehighlight(self, theSurface: Surface, theItemNumber: int, theRect: Rect): if self.highlight_style == 'arrows': self.draw_arrows(theSurface, theRect) else: MultiChoice.draw_prehighlight(self, theSurface, theItemNumber, theRect) def draw_arrows(self, theSurface: Surface, theRect: Rect): """ Args: theSurface: pygame surface to drawn theRect: The pygame rectangle to draw in """ m = self.margin color = self.sel_color or self.fg_color x, y = theRect.midtop pts = [(x - m, y - m), (x + m, y - m), (x, y)] draw.polygon(theSurface, color, pts) x, y = theRect.midbottom y -= 1 pts = [(x - m, y + m), (x + m, y + m), (x, y)] draw.polygon(theSurface, color, pts)
Ancestors
Methods
def draw_arrows(self, theSurface, theRect)
-
Args
theSurface
- pygame surface to drawn
theRect
- The pygame rectangle to draw in
Source code
def draw_arrows(self, theSurface: Surface, theRect: Rect): """ Args: theSurface: pygame surface to drawn theRect: The pygame rectangle to draw in """ m = self.margin color = self.sel_color or self.fg_color x, y = theRect.midtop pts = [(x - m, y - m), (x + m, y - m), (x, y)] draw.polygon(theSurface, color, pts) x, y = theRect.midbottom y -= 1 pts = [(x - m, y + m), (x + m, y + m), (x, y)] draw.polygon(theSurface, color, pts)
def draw_item(self, theSurface, n, theRect)
-
Source code
def draw_item(self, theSurface: Surface, n: int, theRect: Rect): buf = self.font.render(self.labels[n], True, self.fg_color) blit_in_rect(theSurface, buf, theRect, self.align, self.margin)
Inherited members
MultiChoice
:add
add_anchor
add_centered
anchor
attention_lost
augment_mouse_event
bg_color
bg_image
border_color
border_width
call_handler
call_parent_handler
cell_margin
cell_rect
click_item
defer_drawing
dismiss
draw
draw_item_and_highlight
draw_over
draw_posthighlight
draw_prehighlight
enable
enabled
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
highlight_color
highlight_style
highlighted
inherited
invalidate
is_gl_container
item_is_selected
key_down
key_up
local_to_global
margin
menu_bar
num_cols
num_rows
parent
parent_resized
present
rect
ref
relative_mode
remove
remove_anchor
resized
scale_bg
scroll_button_color
scroll_button_size
sel_color
sel_width
set_parent
set_size_for_text
value
visible
PaletteView
: