Debian Codegraph @

traitsui/examples/demo/Advanced/Table_editor_with_checkbox_column.py

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