traitsui prevent txt-box from realtime updating

梦想与她 提交于 2019-12-08 07:37:42

问题


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.

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.

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.

thanks,

alex


回答1:


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()

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



来源:https://stackoverflow.com/questions/21885345/traitsui-prevent-txt-box-from-realtime-updating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!