问题描述
经常使用traitsui时,我有一个depends_on
关系,该关系有些昂贵,并且我会不使用文本框的每个字符输入都不更新该特征.
Frequently when using traitsui, i have a depends_on
relationships that are somewhat costly, and i would to Not update the trait with every character entry of a text-box.
例如,如果我有一个用于计算的Int,则通过depends_on链接,每当我向Int添加一个数字时,该链接便被激活.
For example, if i have a Int which is used in a calculation, through depends_on linkage, the linkage is actuated every time i add a digit to Int.
目前,我通过使用按钮来规避此问题,但我是否对是否有一种方法可以推迟更新所有内容直到用户点击enter
或更改UI的焦点感到好奇.
currently i circumvent this by using buttons, but am curious if there is there a way to defer updating everything until the user hits enter
or changes focus of the UI.
谢谢
alex
推荐答案
您只需要将要创建的几个关键字参数auto_set
和enter_set
指定给TextEditor,以便小部件知道何时需要评估您的价值.这是一个实现惰性小部件的示例:
You simply need to specify to the TextEditor you are creating a couple of keyword arguments auto_set
and enter_set
so that the widget knows when it needs to evaluate your value. Here would be an example implementing a lazy widget:
from traits.api import HasTraits, Int
from traitsui.api import TextEditor, View, Item
class LazyEval(HasTraits):
a = Int
# Additional 'evaluate' is needed to make an int from a string
view = View(Item("a", editor=TextEditor(auto_set=False, enter_set=True,
evaluate=int)))
def _a_changed(self):
print "New value of a is %s" % self.a
l = LazyEval()
l.configure_traits()
此代码将仅在您按Enter时打印一个值.如果删除了auto_set和enter_set,它将在每次击键时将其打印出来.有关更多详细信息: http://docs.enthought.com/traitsui/traitsui_user_man_manual/factories_basic .html#texteditor
This code will print values for a only when you hit enter now. If you removed auto_set and enter_set, it would print it on every keystroke. For more details: http://docs.enthought.com/traitsui/traitsui_user_manual/factories_basic.html#texteditor
这篇关于traitsui阻止txt-box实时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!