简体   繁体   English

在特质工厂使用方法特质

[英]using a method trait in a traitsui factory

I am trying to figure out how Method traits work and if I can use one in a factory defined in a traits View object in order to have dynamic values passed to the factory. 我试图弄清楚Method特征如何工作,以及是否可以在特征视图对象中定义的工厂中使用它,以便将动态值传递给工厂。

Here is what I mean, a minimal test case which works except for the factory behavior (it runs, but using the factory will cause a crash): 这就是我的意思,除了工厂行为(运行,但使用工厂会导致崩溃)之外,一个最小的测试用例有效:

from traits.api import HasTraits, Method, Str, List, Instance, method, Int, Any
from traitsui.api import View, Item, TableEditor, ObjectColumn, TextEditor

class Dog(HasTraits):
    name = Str('Fido')
    value = Int(5)

class DogTable(HasTraits):

    def factory(self):
        return Dog(value=self.current_user_value)    

    dog_factory = Method(factory)

    dogs = List(Instance(Dog))
    current_user_value = Int(3)

    def _dogs_default(self):
        return [
            Dog(name='Joe', value=6),
            Dog(name='Ted', value=2),
        ]

    traits_view = View(
        Item('dogs',
            editor=TableEditor( columns =
                [
                ObjectColumn(label='name', editor=TextEditor(), name='name'),
                ObjectColumn(label='value', editor=TextEditor(), name='value'),
                ],
               row_factory = dog_factory,
            )
        ),
        Item('current_user_value'),
        height=300, width=300,
    )

DogTable().configure_traits()

So what I am trying to do here is set up the factory so that the user can add new items to the table which contain as an initial value whatever value is currently specified by the user in the GUI. 因此,我在这里要做的是设置工厂,以便用户可以将新项目添加到表中,其中包含用户当前在GUI中指定的任何值作为初始value

Is there some way to do this? 有什么办法可以做到这一点? I thought that using a Method trait would resolve this problem by referring to the bound method and allow me to actually call the bound method in this instance, but it seems like Method 's semantics are no different from Callable . 我以为使用Method特质可以通过引用bound方法来解决此问题,并允许我在此实例中实际调用bound方法,但是似乎Method的语义与Callable并无不同。 And I can't figure out any way to supply arguments to the factory dynamically, other possibly than by hacky use of eval or global variables ( factory_row_args rejects dynamic arguments). 而且,除了通过恶意使用eval或全局变量( factory_row_args拒绝动态参数)之外,我无法找到任何动态地向工厂提供参数的方法。

Yesterday I was very tired, today I thought of an obvious way to do it: 昨天我很累,今天我想到了一种显而易见的方法:

def dog_factory(self):
    return Dog(value=self.current_user_value)

def view_getter(self):
    return  View(
        Item('dogs',
            editor=TableEditor( columns =
                [
                ObjectColumn(label='name', editor=TextEditor(), name='name'),
                ObjectColumn(label='value', editor=TextEditor(), name='value'),
                ],  
               row_factory = self.dog_factory
            )
        ),
        Item('current_user_value'),
        height=300, width=300,
    )

def configure_traits(self):
    super(DogTable, self).configure_traits(view=self.view_getter())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM