0

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

我一直在尝试自己解决这个问题太久了,所以我想在这里向专家寻求一些友好的建议:)

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

基本上,我可以使用 TabularAdapter 将表格编辑器生成到 dtypes 数组中,它工作得很好,除了:

1) 每当我更改元素的数量(标识为“骨折数:”)时,数组都会调整大小,但在我单击其中一个元素之前,表格不会反映更改。我想要发生的是,在我释放骨折数滑块后,行数(骨折数)会发生变化。这是可行的吗?

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

2b)我以为我找到了一种方法让表编辑器显示完整数组,即使它增加了代码中的 5 个集合(就在调用 .trait_configure() 之前),但我被愚弄了:( 我尝试添加另一个Group() 在 vertical_fracture_group 前面,所以表格不是第一个显示的东西。这更接近地模拟了我的整个程序。当我这样做时,我被锁定在新的较小大小的数组中,我不能再将其大小增加到最大 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 的讨论中,他提出了一些迄今为止对我没有用的建议:(以下是我的“当前”版本的测试代码,以便 Chris(或其他任何希望加入的人)可以查看我是否我犯了一些明显的错误。

# -*- 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
4

1 回答 1

5

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

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

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

编辑

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

# -*- 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()
于 2015-01-08T15:19:23.563 回答