from traits.api import HasTraits, Instance, List, Str
from traitsui.api import Item, View, VGroup, EnumEditor,TableEditor, ObjectColumn
keys = ["key1", "key2", "key3"]
ph_dict={}
ph_dict["key1"] = ["A1","A2","A3"]
ph_dict["key2"] = ["B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8"]
ph_dict["key3"] = ["C1", "C2","C3", "C4", "C5"]
shapes = ["square", "circle","inverted_triangle", "diamond", "dot", "cross", "plus",
"pixel"]
class ShapeTable(HasTraits):
pass
class DynamicTable(HasTraits):
Shape = Str
Shapedropdownlist = List(Str)
shapes_name = List(Str)
shape_table = List(Instance(ShapeTable))
shape_columns = [ObjectColumn(name="Name")]
shape_columns.append(ObjectColumn(name="Value"))
shape_table_editor=TableEditor(columns=shape_columns,
deletable = True,
sortable = False,
sort_model = False,
show_lines = True,
line_color = "black",
editable= False,
show_column_labels = False)
traits_view = View(VGroup(
Item('Shape',editor=EnumEditor(name='Shapedropdownlist',),),
Item( 'shape_table',editor=shape_table_editor,show_label=False,
width=0.3, padding=0)),
width=500, height=200, resizable=True,
title = "Dynamic table trait"
)
def __init__(self):
super(DynamicTable, self).__init__()
self.shapes_name = ph_dict[keys[1]]
self.shape_table_editor.columns = [ObjectColumn(name='name')]
for i in range(len(shapes)):
self.shape_table_editor.columns.append(ObjectColumn(name=shapes[i]))
self.Shapedropdownlist = keys
self.Shape = keys[2]
self._updateShapeTable()
return
def _Shape_changed(self, selectedValue):
self.pshape = selectedValue
self.shapes_name = ph_dict[self.pshape]
self._updateShapeTable()
def _updateShapeTable(self):
del(self.shape_table)
self.shape_table_editor.columns = [ObjectColumn(name='name')]
for i in range(len(self.shapes_name)):
print(i,len(self.shapes_name))
self.shape_table_editor.columns.append(ObjectColumn(name=shapes[i]))
data = ShapeTable()
data.name = "Name"
for i in range(len(self.shapes_name)):
print(i,len(self.shapes_name))
exec('data.'+shapes[i]+'="'+self.shapes_name[i]+'"')
self.shape_table.append(data)
data = ShapeTable()
data.name = "Shape"
for i in range(len(self.shapes_name)):
exec('data.'+shapes[i]+'="'+shapes[i]+'"')
self.shape_table.append(data)
if __name__ == "__main__":
f1 = DynamicTable()
f1.configure_traits()