Module albow.choices.MultiChoice
Source code
from pygame.locals import K_LEFT
from pygame.locals import K_RIGHT
from albow.widgets.Control import Control
from albow.containers.PaletteView import PaletteView
from albow.themes.ThemeProperty import ThemeProperty
class MultiChoice(PaletteView, Control):
highlight_color = ThemeProperty('highlight_color')
cell_margin = ThemeProperty('cell_margin')
align = 'c'
tab_stop = True
def __init__(self, cell_size, values, **kwds):
PaletteView.__init__(self, cell_size, 1, len(values), **kwds)
self.values = values
def num_items(self):
return len(self.values)
def item_is_selected(self, n):
return self.get_value() == self.values[n]
def click_item(self, n, e):
if self.tab_stop:
self.focus()
self.set_value(self.values[n])
def draw(self, surf):
if self.has_focus():
surf.fill(self.highlight_color)
PaletteView.draw(self, surf)
def key_down(self, e):
k = e.key
if k == K_LEFT:
self.change_value(-1)
elif k == K_RIGHT:
self.change_value(1)
else:
PaletteView.key_down(self, e)
def change_value(self, d):
values = self.values
if values:
n = len(values)
value = self.get_value()
try:
i = values.index(value)
except ValueError:
if d < 0:
i = 0
else:
i = n - 1
else:
i = max(0, min(n - 1, i + d))
self.set_value(values[i])
Classes
class MultiChoice (cell_size, values, **kwds)-
The PaletteView class is an abstract base class for implementing tool palettes and similar things. A PaletteView displays an array of items which can be selected by clicking, with the selected item being highlighted. There is provision for scrolling, so that the palette can contain more items than are displayed at one time.
The PaletteView does not maintain the items themselves or keep track of which one is selected; these things are responsibilities of the subclass.
Initializes the palette view with the specified cell_size, and a rect sized for displaying nrows rows and ncols columns of items. If scrolling is true, controls will be displayed for scrolling the view.
Args
cell_size- A tuple that specifies the cell size (width, height)
nrows-
The # of rows
ncols-
The # of columns
scrolling- True to display scroll bars, else false
**kwds:
Source code
class MultiChoice(PaletteView, Control): highlight_color = ThemeProperty('highlight_color') cell_margin = ThemeProperty('cell_margin') align = 'c' tab_stop = True def __init__(self, cell_size, values, **kwds): PaletteView.__init__(self, cell_size, 1, len(values), **kwds) self.values = values def num_items(self): return len(self.values) def item_is_selected(self, n): return self.get_value() == self.values[n] def click_item(self, n, e): if self.tab_stop: self.focus() self.set_value(self.values[n]) def draw(self, surf): if self.has_focus(): surf.fill(self.highlight_color) PaletteView.draw(self, surf) def key_down(self, e): k = e.key if k == K_LEFT: self.change_value(-1) elif k == K_RIGHT: self.change_value(1) else: PaletteView.key_down(self, e) def change_value(self, d): values = self.values if values: n = len(values) value = self.get_value() try: i = values.index(value) except ValueError: if d < 0: i = 0 else: i = n - 1 else: i = max(0, min(n - 1, i + d)) self.set_value(values[i])Ancestors
Subclasses
Class variables
var align
Instance variables
var cell_margin-
The ThemeProperty class is a property descriptor used for defining theme properties.
Example
class Battlefield(Widget): phaser_color = ThemeProperty('phaser_color') var highlight_color-
The ThemeProperty class is a property descriptor used for defining theme properties.
Example
class Battlefield(Widget): phaser_color = ThemeProperty('phaser_color')
Methods
def change_value(self, d)-
Source code
def change_value(self, d): values = self.values if values: n = len(values) value = self.get_value() try: i = values.index(value) except ValueError: if d < 0: i = 0 else: i = n - 1 else: i = max(0, min(n - 1, i + d)) self.set_value(values[i]) def num_items(self)-
Source code
def num_items(self): return len(self.values)
Inherited members
PaletteView:addadd_anchoradd_centeredanchorattention_lostaugment_mouse_eventbg_colorbg_imageborder_colorborder_widthcall_handlercall_parent_handlercell_rectclick_itemdefer_drawingdismissdrawdraw_item_and_highlightdraw_overdraw_posthighlightdraw_prehighlightfg_colorfocusfocus_switchfontget_cursorget_focusget_margin_rectget_rootget_top_widgetget_visibleglobal_to_localhas_focushighlight_styleinheritedinvalidateis_gl_containeritem_is_selectedkey_downkey_uplocal_to_globalmarginmenu_barnum_colsnum_rowsparentparent_resizedpresentrectrelative_moderemoveremove_anchorresizedscale_bgscroll_button_colorscroll_button_sizesel_colorsel_widthset_parentset_size_for_texttab_stopvisible
Control: