在侦听外部模型对象时启用

在侦听外部模型对象时启用

本文介绍了在侦听外部模型对象时启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了解决原始问题的解决方法,但我希望其他人可以解释正在发生的事情.我最初注意到enabled_when,并且我也想像visible_when似乎对仅源自模型对象的特征事件起了作用.如果事件起源于某个其他对象,即使编辑器引用了该事件,则该事件似乎也无法正确传播.

I found a workaround to my original problem but I am hoping someone else can explain what is going on. I originally noticed that enabled_when, and I'd imagine visible_when also, appears to take effect in response to trait events originating from the model object only. If the event originates from some other object, even if the editor refers to it, it doesn't seem to be propagated correctly.

class DirectObjectPronoun(HasTraits):
    text=Str
    typable=Bool(False)

    traits_view=View(
        Item(name='typable'),
        Item(name='text',enabled_when='typable'))

class IndirectObjectPronoun(HasTraits):
    referent=Instance(DirectObjectPronoun,())

    traits_view=View(
        Item(name='typable',object='object.referent'),
        Item(name='text',object='object.referent',
            enabled_when='object.referent.typable'))

IndirectObjectPronoun().configure_traits()

所需的行为是typable is True时启用文本窗口,否则禁用.观察到的行为是始终禁用文本窗口(尽管如果typable的默认值设置为True,则始终将其启用,因此问题必须出在侦听器中.)

The desired behavior is that the text window is enabled when typable is True and disabled otherwise. The observed behavior is that the text window is always disabled (though if the default value of typable is set to True then it is always enabled, so the problem must be in the listener.)

如果直接编辑DirectObjectPronoun,则禁用将按预期进行.

If a DirectObjectPronoun is edited directly, the disabling works as intended.

我发现了一个我不理解的工作方式.

I found a workaroud that that I don't understand.

class IndirectObjectPronoun(HasTraits):
    stupid_listener=Bool
    referent=Instance(DirectObjectPronoun,())

    traits_view=View(
        Item(name='typable',object='object.referent'),
        Item(name='text',object='object.referent',
            enabled_when='object.referent.typable'))

    @on_trait_change('referent.typable')
    def _stupid_listener_listens_stupidly(self):
        self.stupid_listener=self.referent.typable

这个想法很简单:创建一个愚蠢的变量,只听条件,不做任何事,然后将该局部变量设置为条件.

The idea is pretty simple: make a stupid variable that does nothing but listen to the condition, and then set that local variable to be the condition.

但是,当我对此进行测试时,我忘记了更改enabled_when,但是无论如何它都能正常工作.以某种方式,添加此侦听器似乎已经提醒IndirectObjectPronoun它应该仍然侦听此变量.似乎_stupid_listener_listens_stupidly函数的内容也很重要-如果将其更改为passprint 56或其他内容,它将不再起作用.

However, when I was testing this I forgot to change enabled_when but it worked correctly anyway. In some way, adding this listener seems to have reminded the IndirectObjectPronoun that it's supposed to listen to this variable anyway. It also appears that the content of the _stupid_listener_listens_stupidly function matters -- if you change this to pass or print 56 or whatever, it no longer works.

有人知道这是怎么回事吗?

Does anyone know what's going on here?

推荐答案

如果不研究源代码,我不知道为什么它不起作用.至少,您描述的不一致看起来是错误的.

Without studying the source, I don't know why it doesn't work; at very least, the inconsistency that you describe seems wrong.

更直观的解决方法/解决方案是使用委托:

A more intuitive workaround/solution is to use delegation:

class IndirectObjectPronoun(HasTraits):
    referent=Instance(DirectObjectPronoun,())
    typable = DelegatesTo('referent')

    traits_view=View(
        Item(name='typable',object='object.referent'),
        Item(name='text',object='object.referent',
             enabled_when='typable'))

这篇关于在侦听外部模型对象时启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:14