python特征:表中的按钮列

时间:2013-02-26 13:18:30

标签: python traits enthought traitsui

我想在表格的每一行的末尾添加一个Button。

关闭窗口时,以下代码会导致PyDeadObjectError:

from traits.api import HasTraits,Str,Int,Button,Instance
from traitsui.api import TableEditor,ObjectColumn,View
class Person(HasTraits):
    name=Str
    age=Int
    Plot_size=Button(label='Plot size')

class Display(HasTraits):
    table=List(Instance(Person))
    table_editor=TableEditor(columns=[ObjectColumn(name='name'),
        ObjectColumn(name='age'),
        ObjectColumn(name='Plot_size')],
        deletable = True,
        sortable = False,
        sort_model = False,
        show_lines = True,
        orientation = 'vertical',
        show_column_labels = True)
    traits_view=View(Item('table',editor=table_editor),resizable=True)

a=Display()
a.table.append(Person(name='Joe',age=21))
a.table.append(Person(name='John',age=27))
a.table.append(Person(name='Jenny',age=23))
a.configure_traits()

有人已经尝试过这样做吗?我怎样才能摆脱这个错误?即使不点击相应的单元格,是否可以显示按钮?

1 个答案:

答案 0 :(得分:1)

我不确定那里有什么问题,但也许有一种解决方法。不是让一列满了按钮,而是有一个按钮,然后使用选定的行。

from traits.api import HasTraits,Str,Int,Button,Instance, List
from traitsui.api import TableEditor,ObjectColumn,View, Item

class Person(HasTraits):
    name=Str
    age=Int
    #Plot_size=Button(label='Plot size')


class Display(HasTraits):
    Plot_size=Button(label='Plot size')
    selected_person = Instance(Person)

    people=List(Instance(Person))
    table_editor=TableEditor(columns=[ObjectColumn(name='name'),
        ObjectColumn(name='age')],
        selected='selected_person',
        #ObjectColumn(name='Plot_size', editable=False)],
        deletable = True,
        sortable = False,
        sort_model = False,
        show_lines = True,
        orientation = 'vertical',
        show_column_labels = True)

    traits_view=View(Item('people',editor=table_editor),
            Item('Plot_size'),
            resizable=True)

    def _Plot_size_fired(self):
            print self.selected_person.name

demo=Display(people = [Person(name='Joe',age=21),
    Person(name='John',age=27),
    Person(name='Jenny',age=23)])

if __name__ == '__main__':
    demo.configure_traits()

否则,也许checkbox_column示例是一个很好的起点。