1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
# Copyright (c) 2007, Enthought, Inc.
# License: BSD Style.
"""
This shows a table editor which has a checkbox column in addition to normal
data columns.
"""
# Imports:
from __future__ import absolute_import
from random \
import randint
from traits.api \
import HasStrictTraits, Str, Int, Float, List, Bool, Property
from traitsui.api \
import View, Item, TableEditor
from traitsui.table_column \
import ObjectColumn
from traitsui.extras.checkbox_column \
import CheckboxColumn
# Create a specialized column to set the text color differently based upon
# whether or not the player is in the lineup:
class PlayerColumn(ObjectColumn):
# Override some default settings for the column:
width = 0.08
horizontal_alignment = 'center'
def get_text_color(self, object):
return ['light grey', 'black'][object.in_lineup]
# The 'players' trait table editor:
player_editor = TableEditor(
sortable=False,
configurable=False,
auto_size=False,
selected_indices='selected_player_indices',
columns=[CheckboxColumn(name='in_lineup', label='In Lineup',
width=0.12),
PlayerColumn(name='name', editable=False, width=0.24,
horizontal_alignment='left'),
PlayerColumn(name='at_bats', label='AB'),
PlayerColumn(name='strike_outs', label='SO'),
PlayerColumn(name='singles', label='S'),
PlayerColumn(name='doubles', label='D'),
PlayerColumn(name='triples', label='T'),
PlayerColumn(name='home_runs', label='HR'),
PlayerColumn(name='walks', label='W'),
PlayerColumn(name='average', label='Ave',
editable=False, format='%0.3f')])
# 'Player' class:
class Player(HasStrictTraits):
# Trait definitions:
in_lineup = Bool(True)
name = Str
at_bats = Int
strike_outs = Int
singles = Int
doubles = Int
triples = Int
home_runs = Int
walks = Int
average = Property(Float)
def _get_average(self):
""" Computes the player's batting average from the current statistics.
"""
if self.at_bats == 0:
return 0.0
return float(self.singles + self.doubles +
self.triples + self.home_runs) / self.at_bats
class Team(HasStrictTraits):
# Trait definitions:
players = List(Player)
selected_player_indices = List
# Trait view definitions:
traits_view = View(
Item('players',
show_label=False,
editor=player_editor
),
title='Baseball Team Roster Demo',
width=0.5,
height=0.5,
resizable=True
)
def random_player(name):
""" Generates and returns a random player.
"""
p = Player(name=name,
strike_outs=randint(0, 50),
singles=randint(0, 50),
doubles=randint(0, 20),
triples=randint(0, 5),
home_runs=randint(0, 30),
walks=randint(0, 50))
return p.trait_set(
at_bats=p.strike_outs +
p.singles +
p.doubles +
p.triples +
p.home_runs +
randint(
100,
200))
# Create the demo:
demo = view = Team(players=[random_player(name) for name in [
'Dave', 'Mike', 'Joe', 'Tom', 'Dick', 'Harry', 'Dirk', 'Fields', 'Stretch'
]])
# Run the demo (if invoked from the command line):
if __name__ == '__main__':
demo.configure_traits()
|