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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
"""
This shows a table editor which has a checkbox column in addition to normal
data columns.
"""
from random import randint
from traits.api import HasStrictTraits, Str, Int, Float, List, Bool, Property
from traitsui.api import Item, ObjectColumn, TableEditor, View
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):
if object.in_lineup:
return 'black'
return 'light grey'
# 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'
),
],
)
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 = 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()
|