如何使edit_traits()GUI项目响应其依赖项的变化? [英] How do I make an edit_traits() GUI item responsive to changes in its dependencies?

查看:137
本文介绍了如何使edit_traits()GUI项目响应其依赖项的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计具有依赖属性的HasTraits子类:

I'm designing a HasTraits subclass with dependent properties:

#!/usr/bin/env python

# Example for SO question on dynamically changing Dict contents.
from traits.api   import HasTraits, Dict, Property, Trait, Int, cached_property
from traitsui.api import View, Item

class Foo(HasTraits):
    "Has dependent properties, which I'd like to remain up-to-date in the GUI."

    _dicts = [
        {"zero": 0, "one": 1},
        {"zero": 1, "one": 2},
        {"zero": 2, "one": 3},
    ]

    zap = Int(0)
    bar = Property(Trait, depends_on=["zap"])
    baz = Trait(list(_dicts[0])[0], _dicts[0])

    @cached_property
    def _get_bar(self):
        return Trait(list(self._dicts)[self.zap], self._dicts)

    traits_view = View(
        Item("zap"),
        Item("bar"),
        Item("baz"),
        width=500,
        )

if __name__ == '__main__':
    Foo().configure_traits()

运行此代码时,我看到:

When I run this code I see:

如果我更改Zap的值:

请注意以下几点:

  1. 更改Zap后,Bar的地址已更改.

这意味着对Bar的更改将在GUI仍处于打开状态时动态更新.那太棒了!但是...

This means that changes to Bar are being dynamically updated in the GUI, while it's still opened; that's great! However...

Bar在GUI中的显示方式不是很有用.

The way Bar is displayed in the GUI is not very useful.

我希望将Bar显示为Baz:可由用户选择.

I'd love to have Bar displayed as Baz is displayed: selectable by the user.

我想要两全其美:

    我用Bar
  • 看到的
  • 动态GUI更新
  • Baz的显示格式.
  • the dynamic GUI updating I see with Bar, and
  • the display format of Baz.

有人知道我怎么能得到这个吗?

Does anyone know how I can get this?

我尝试了几种方法来动态更新类似Baz的项目,但无济于事. (请参阅此以前的版本这样的问题.)

I've tried several ways of updating a Baz-like item dynamically, to no avail. (See this previous SO question.)

推荐答案

以下代码使我获得了想要的行为:

The following code gets me the behavior I want:

#!/usr/bin/env python

# Example for SO question on dynamically changing Dict contents.
from traits.api   import HasTraits, Dict, Property, Trait, Int, cached_property, Enum, List
from traitsui.api import View, Item

class Foo(HasTraits):
    "Has dependent properties, which I'd like to remain up-to-date in the GUI."

    _dict = {
        "zero": 0,
        "one":  1,
        "two":  2,
    }

    _zaps = [
        ["zero", "one"],        
        ["one",  "two"],
        ["zero", "two"],
    ]
    zaps = List(_zaps[0])
    zap  = Enum([0,1,2])  # Selection of `zap` should limit the items of `_dict` available for selection.
    bar  = Enum(_zaps[0][0], values="zaps")
    bar_ = Int(_dict[_zaps[0][0]])

    def _zap_changed(self, new_value):
        self.zaps = self._zaps[new_value]
        self.bar_ = self._dict[self.bar]

    def _bar_changed(self, new_value):
        self.bar_ = self._dict[self.bar]

    traits_view = View(
        Item("zap"),
        Item("bar"),
        Item("bar_", style="readonly"),
        width=500,
        )

if __name__ == '__main__':
    Foo().configure_traits()

程序启动后立即

在将Zap更改为"1"后:

这篇关于如何使edit_traits()GUI项目响应其依赖项的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆