TabularAdapter 编辑器问题

TabularAdapter Editor issue

我遇到了 TraitsUI 包中的 TabularAdapter 问题...

我已经尝试自己解决这个问题太久了,所以我想向这里的专家请教一些友好的建议:)

我将添加一段程序来说明我的问题,我希望有人可以查看它并说“啊哈!......这是你的问题”(我的手指交叉)。

基本上,我可以使用 TabularAdapter 将 table 编辑器生成到 dtype 数组中,它工作正常,除了:

1) 每当我更改元素的数量(标识为 'Number of fractures:')时,数组都会调整大小,但 table 直到我单击其中一个后才会反映更改元素。我想要发生的是,在我释放 # of fractures 滑块后,# of rows (fractions) 会发生变化。这可行吗?

2) 我遇到的第二个问题是,如果数组在 .configure_traits() 显示之前调整大小(当 Number_of_fractures 被修改时由通知程序显示),我可以缩小大小数组的大小,但我无法将其增加到新大小。

2b) 我想我找到了一种方法让 table 编辑器显示完整的数组,即使它增加到超过代码中的 5 个集合(就在调用 .trait_configure( 之前) )), 但我被愚弄了 :( 我尝试在 vertical_fracture_group 前面添加另一个 Group() 所以 table 不是第一个显示的东西。这更接近地模拟了我的整个程序。当我这样做了,我被锁定在新的较小尺寸的数组中,我无法再将其尺寸增加到最大值 15。我正在修改代码以反映此问题。

这是我的示例代码:

# -*- coding: utf-8 -*-
"""
This is a first shot at developing a ****** User Interface using Canopy by
Enthought.  Canopy is a distribution of the Python language which has a lot of
scientific and engineering features 'built-in'.
"""


#-- Imports --------------------------------------------------------------------

from traitsui.api import TabularEditor
from traitsui.tabular_adapter import TabularAdapter
from numpy import zeros, dtype

from traits.api import HasTraits,  Range

from traitsui.api import View, Group, Item

#-- FileDialogDemo Class -------------------------------------------------------

max_cracks = 15     #maximum number of Fracs/cracks to allow

class VertFractureAdapter(TabularAdapter):
    columns = [('Frac #',0), ('X Cen',1), ('Y Cen',2), ('Z Cen',3),
        ('Horiz',4), ('Vert',5), ('Angle',6)]



class SetupDialog ( HasTraits ):
    Number_Of_Fractures = Range(1, max_cracks) # line 277

    vertical_frac_dtype = dtype([('Fracture', 'int'), ('x', 'float'), ('y', 'float'),
            ('z', 'float'), ('Horiz Length', 'float'), ('Vert Length', 'float')
            , ('z-axis Rotation, degrees', 'float')])
    vertical_frac_array = zeros((max_cracks), dtype=vertical_frac_dtype)

    vertical_fracture_group = Group(
        Item(name = 'vertical_frac_array',
            show_label = False,
            editor     = TabularEditor(adapter = VertFractureAdapter()),
            width = 0.5,
            height = 0.5,
        )
    )


    #-- THIS is the actual 'View' that gets put on the screen
    view = View(
        #Note: When as this group 'displays' before the one with the Table, I'm 'locked' into my new maximum table display size of 8 (not my original/desired maximum of 15)
        Group(
            Item( name = 'Number_Of_Fractures'),
        ),

        #Note: If I place this Group() first, my table is free to grow to it's maximum of 15
        Group(
            Item( name = 'Number_Of_Fractures'),
            vertical_fracture_group,
        ),

        width = 0.60,
        height = 0.50,
        title = '****** Setup',
        resizable=True,
    )


    #-- Traits Event Handlers --------------------------------------------------
    def _Number_Of_Fractures_changed(self):
        """ Handles resizing arrays if/when the number of Fractures is changed"""
        print "I've changed the # of Fractures to " + repr(self.Number_Of_Fractures)
        #if not self.user_StartingUp:
        self.vertical_frac_array.resize(self.Number_Of_Fractures, refcheck=False)

        for crk in range(self.Number_Of_Fractures):
            self.vertical_frac_array[crk]['Fracture'] = crk+1
            self.vertical_frac_array[crk]['x'] = crk
            self.vertical_frac_array[crk]['y'] = crk
            self.vertical_frac_array[crk]['z'] = crk



# Run the program (if invoked from the command line):
if __name__ == '__main__':
    # Create the dialog:
    fileDialog = SetupDialog()

    fileDialog.configure_traits()

    fileDialog.Number_Of_Fractures = 8

在下面我与 Chris 的讨论中,他提出了一些迄今为止对我没有用的建议 :( 以下是我的 'current' 版本的测试代码,因此 Chris(或任何其他希望发出声音的人) in) 可以看到我是否犯了一些明显的错误。

# -*- coding: utf-8 -*-
"""
This is a first shot at developing a ****** User Interface using Canopy by
Enthought.  Canopy is a distribution of the Python language which has a lot of
scientific and engineering features 'built-in'.
"""


#-- Imports --------------------------------------------------------------------

from traitsui.api import TabularEditor
from traitsui.tabular_adapter import TabularAdapter
from numpy import zeros, dtype

from traits.api import HasTraits,  Range, Array, List

from traitsui.api import View, Group, Item

#-- FileDialogDemo Class -------------------------------------------------------

max_cracks = 15     #maximum number of Fracs/cracks to allow

class VertFractureAdapter(TabularAdapter):
    columns = [('Frac #',0), ('X Cen',1), ('Y Cen',2), ('Z Cen',3),
        ('Horiz',4), ('Vert',5), ('Angle',6)]
    even_bg_color = 0xf4f4f4 # very light gray



class SetupDialog ( HasTraits ):
    Number_Of_Fractures = Range(1, max_cracks) # line 277
    dummy = Range(1, max_cracks)

    vertical_frac_dtype = dtype([('Fracture', 'int'), ('x', 'float'), ('y', 'float'),
            ('z', 'float'), ('Horiz Length', 'float'), ('Vert Length', 'float')
            , ('z-axis Rotation, degrees', 'float')])
    vertical_frac_array = Array(dtype=vertical_frac_dtype)

    vertical_fracture_group = Group(
        Item(name = 'vertical_frac_array',
            show_label = False,
            editor     = TabularEditor(adapter = VertFractureAdapter()),
            width = 0.5,
            height = 0.5,
        )
    )


    #-- THIS is the actual 'View' that gets put on the screen
    view = View(
        Group(
            Item( name = 'dummy'),
        ),

        Group(
            Item( name = 'Number_Of_Fractures'),
            vertical_fracture_group,
        ),

        width = 0.60,
        height = 0.50,
        title = '****** Setup',
        resizable=True,
    )


    #-- Traits Event Handlers --------------------------------------------------
    def _Number_Of_Fractures_changed(self, old, new):
        """ Handles resizing arrays if/when the number of Fractures is changed"""
        print "I've changed the # of Fractures to " + repr(self.Number_Of_Fractures)
        vfa = self.vertical_frac_array
        vfa.resize(self.Number_Of_Fractures, refcheck=False)

        for crk in range(self.Number_Of_Fractures):
            vfa[crk]['Fracture'] = crk+1
            vfa[crk]['x'] = crk
            vfa[crk]['y'] = crk
            vfa[crk]['z'] = crk

        self.vertical_frac_array = vfa



# Run the program (if invoked from the command line):
if __name__ == '__main__':
    # Create the dialog:
    fileDialog = SetupDialog()

    # put the actual dialog up...if I put it up 'first' and then resize the array, I seem to get my full range back :)
    fileDialog.configure_traits()

    #fileDialog.Number_Of_Fractures = 8

有两个代码细节导致您描述的问题。首先,vertical_frac_array 不是特征,因此表格编辑器无法监视它的变化。因此,table 仅在您手动与之交互时刷新。其次,traits 不监视数组内容的变化,而是监视数组的标识。因此,将不会检测到调整大小和将值分配到数组中。

解决此问题的一种方法是首先创建 vertical_frac_arrayArray 特征。例如。 vertical_frac_array = Array(dtype=vertical_frac_dtype)。然后,在 _Number_Of_Fractures_changed 中,不要 resize vertical_frac_array 并就地修改它。相反,复制 vertical_frac_array、调整大小、修改内容,然后将操纵的副本重新分配回 vertical_frac_array。这样 table 将看到数组的标识已更改并刷新视图。

另一种选择是将 vertical_frac_array 设为 List 而不是 Array。这避免了上面的复制和重新分配技巧,因为特征确实监视列表的内容。

编辑

我的解决方案如下。我没有在 Number_Of_Fractures 更改时调整 vertical_frac_array 的大小,而是重新创建数组。我还通过 _vertical_frac_array_default 方法为 vertical_frac_array 提供了默认值。 (我也从视图中删除了不必要的代码。)

# -*- coding: utf-8 -*-
"""
This is a first shot at developing a ****** User Interface using Canopy by
Enthought.  Canopy is a distribution of the Python language which has a lot of
scientific and engineering features 'built-in'.
"""


#-- Imports --------------------------------------------------------------------

from traitsui.api import TabularEditor
from traitsui.tabular_adapter import TabularAdapter
from numpy import dtype, zeros

from traits.api import HasTraits,  Range, Array

from traitsui.api import View, Item

#-- FileDialogDemo Class -------------------------------------------------------

max_cracks = 15     #maximum number of Fracs/cracks to allow

vertical_frac_dtype = dtype([('Fracture', 'int'), ('x', 'float'), ('y', 'float'),
        ('z', 'float'), ('Horiz Length', 'float'), ('Vert Length', 'float')
        , ('z-axis Rotation, degrees', 'float')])


class VertFractureAdapter(TabularAdapter):
    columns = [('Frac #',0), ('X Cen',1), ('Y Cen',2), ('Z Cen',3),
        ('Horiz',4), ('Vert',5), ('Angle',6)]


class SetupDialog ( HasTraits ):

    Number_Of_Fractures = Range(1, max_cracks) # line 277
    vertical_frac_array = Array(dtype=vertical_frac_dtype)

    view = View(
        Item('Number_Of_Fractures'),
        Item(
            'vertical_frac_array',
            show_label=False,
            editor=TabularEditor(
                adapter=VertFractureAdapter(),
            ),
            width=0.5,
            height=0.5,
        ),
        width=0.60,
        height=0.50,
        title='****** Setup',
        resizable=True,
    )

    #-- Traits Defaults -------------------------------------------------------

    def _vertical_frac_array_default(self):
        """ Creates the default value of the `vertical_frac_array`. """
        return self._calculate_frac_array()

    #-- Traits Event Handlers -------------------------------------------------

    def _Number_Of_Fractures_changed(self):
        """ Update `vertical_frac_array` when `Number_Of_Fractures` changes """
        print "I've changed the # of Fractures to " + repr(self.Number_Of_Fractures)
        #if not self.user_StartingUp:
        self.vertical_frac_array = self._calculate_frac_array()

    #-- Private Interface -----------------------------------------------------

    def _calculate_frac_array(self):
        arr = zeros(self.Number_Of_Fractures, dtype=vertical_frac_dtype)
        for crk in range(self.Number_Of_Fractures):
            arr[crk]['Fracture'] = crk+1
            arr[crk]['x'] = crk
            arr[crk]['y'] = crk
            arr[crk]['z'] = crk
        return arr


# Run the program (if invoked from the command line):
if __name__ == '__main__':
    # Create the dialog:
    fileDialog = SetupDialog()

    fileDialog.configure_traits()