在python中动态更改表的长度

时间:2013-09-17 02:27:53

标签: python traits traitsui

我编写了以下python程序,从列表中选择可变数量的形状(n = 3,8,5),并在选择时生成并显示一个表(n + 1 = 4,9,6)组合框下拉列表中的三个不同的键。问题是表列的数量不会随所选键的变化而固定在其初始设置,但是这些固定列的表内容会更新。您显示变量的种类建议长度表非常感谢。

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()

1 个答案:

答案 0 :(得分:0)

在指定columns_name的列时,您可以使用columns参数代替TableEditor参数。

_updateShapeTable方法中,您应初始化shape_columns上下文属性,而不是将列附加到其中。

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 = List(Instance(ObjectColumn()))  
    shape_table_editor=TableEditor(columns_name='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')]

        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_columns = [ObjectColumn(name='name')]
        for i in range(len(self.shapes_name)):
            print(i,len(self.shapes_name))
            self.shape_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()