Python Traits: 表格中的按钮列
我想在表格的每一行末尾添加一个按钮。
下面的代码在关闭窗口时出现了一个叫做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 个回答
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的例子开始学习,这也是个不错的选择。