2

我是一名新手学生,使用 TraitsUI 模块来可视化数组特征。为了更新 Array 的值,我对演示代码做了一些修改,我收到了消息“IndexError: invalid index to scalar variable”

顺便说一句,我在 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 位。特质ui-4.2.0

4

0 回答 0