问题
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