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
: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_rect
click_item
defer_drawing
dismiss
draw
draw_item_and_highlight
draw_over
draw_posthighlight
draw_prehighlight
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_style
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
relative_mode
remove
remove_anchor
resized
scale_bg
scroll_button_color
scroll_button_size
sel_color
sel_width
set_parent
set_size_for_text
tab_stop
visible
Control
: