在ArrayViewEditor中更改数组特征值时出现python错误

时间:2012-12-07 15:14:58

标签: python traitsui

我是一名新手学生,使用TraitsUI模块来识别数组特征。我对演示代码做了一些修改,以便更新Array的值,我收到了消息 “IndexError:标量变量索引无效”

顺便说一句,我在ArrayEditor中使用相同的方法,它起作用了......

此错误的原因是什么?

是否有正确的方法在ArrayViewEditor中启用Array值的更新?

以下是练习代码:

from numpy.random import random
from traits.api import HasTraits, Array,Button
from traitsui.api import View, Item
from traitsui.ui_editors.array_view_editor import ArrayViewEditor
import numpy as np

#-- ShowArray demo class -------------------------------------------------------

class ShowArray(HasTraits):

data = Array(editor = ArrayViewEditor(titles = [ 'x', 'y', 'z' ],
                                       format = '%.4f',
                                       show_index = False
                                       # Font fails with wx in OSX;
                                       #   see traitsui issue #13:
                                       # font   = 'Arial 8'
                                      ))
change = Button()
view = View(
    Item('data',show_label = False),
    Item('change',show_label = False),
    title     = 'Array Viewer',
    width     = 0.3,
    height    = 0.8,
    resizable = True
)
def _change_fired(self):
    self.data = np.arange(1,4)
#-- Run the demo ---------------------------------------------------------------

# Create the demo:
demo = ShowArray(data = random((100000, 3)))

# Run the demo (if invoked from the command line):
if __name__ == '__main__':
    demo.configure_traits()

我执行了代码,然后按“更改”按钮,出现错误:

Traceback (most recent call last):
File "C:\Python27\lib\site-packages\traitsui\wx\tabular_editor.py", line 167, in OnGetItemText
row, column )
File "C:\Python27\lib\site-packages\traitsui\tabular_adapter.py", line 323, in get_text
return self._result_for( 'get_text', object, trait, row, column )
File "C:\Python27\lib\site-packages\traitsui\tabular_adapter.py", line 574, in _result_for
return handler()
File "C:\Python27\lib\site-packages\traitsui\tabular_adapter.py", line 582, in <lambda>
return lambda: getattr( self, name )
File "C:\Python27\lib\site-packages\traitsui\tabular_adapter.py", line 412, in _get_text
self.object, self.name, self.row, self.column )
File "C:\Python27\lib\site-packages\traitsui\tabular_adapter.py", line 329, in get_content
return self._result_for( 'get_content', object, trait, row, column )
File "C:\Python27\lib\site-packages\traitsui\tabular_adapter.py", line 574, in _result_for
return handler()
File "C:\Python27\lib\site-packages\traitsui\tabular_adapter.py", line 582, in <lambda>
return lambda: getattr( self, name )
File "C:\Python27\lib\site-packages\traitsui\ui_editors\array_view_editor.py", line 53, in _get_content
return self.item[ self.column_id ]
IndexError: invalid index to scalar variable.

Python 2.7.3,32位。 traitsui-4.2.0

0 个答案:

没有答案