python特性:表中的按钮列

python特性:表中的按钮列,python,traits,enthought,traitsui,Python,Traits,Enthought,Traitsui,我想在表中每一行的末尾添加一个按钮 以下代码在关闭窗口时导致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')

我想在表中每一行的末尾添加一个按钮

以下代码在关闭窗口时导致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()

已经有人试过这样做了吗?我怎样才能消除这个错误?即使不点击相应的单元格也可以显示按钮吗?

我不确定问题出在哪里,但可能有解决办法。使用一个按钮,然后使用选定的行,而不是一列按钮

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

否则,可能复选框列示例是一个很好的起点。

我不确定问题出在哪里,但可能有解决方法。使用一个按钮,然后使用选定的行,而不是一列按钮

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

否则,可能复选框列示例是一个很好的起点。

我查看了复选框列,可以找到解决方法(显示复选框而不是按钮)。然而,基于CheckboxColumn编写“ButtonColumn”类似乎相当困难,因为CheckboxColumn使用了一个特殊的呈现器,在我看来,它不适用于Button类。我想我必须更深入地挖掘代码才能让ButtonColumn工作……是的,我想修改CheckboxColumn,但看起来工作量很大。我认为问题在于按钮是一种事件特征,因此不存储值。在表中显示没有值的内容是一个奇怪的想法,尽管我可以理解为什么您可能希望在表中显示按钮。祝你好运Thx…我想我会尝试用checkboxcolumn和表顶部的一个按钮来制定一个解决方案,以便在所有选中的行上执行我想要的操作。干杯我查看了checkbox_列,可以找到一个解决方法(显示一个复选框而不是按钮)。然而,基于CheckboxColumn编写“ButtonColumn”类似乎相当困难,因为CheckboxColumn使用了一个特殊的呈现器,在我看来,它不适用于Button类。我想我必须更深入地挖掘代码才能让ButtonColumn工作……是的,我想修改CheckboxColumn,但看起来工作量很大。我认为问题在于按钮是一种事件特征,因此不存储值。在表中显示没有值的内容是一个奇怪的想法,尽管我可以理解为什么您可能希望在表中显示按钮。祝你好运Thx…我想我会尝试用checkboxcolumn和表顶部的一个按钮来制定一个解决方案,以便在所有选中的行上执行我想要的操作。干杯我注意到,如果用户在关闭窗口之前试图与“Plot_size”列交互,则仅在关闭时生成PyDeadObjectError。对此有何想法?我注意到,如果用户试图在关闭窗口之前与“Plot_size”列交互,则仅在关闭时生成PyDeadObjectError。有什么想法吗?