科学计算三维可视化---Traits(Event和button属性)

Event和button属性

是两个专门用于处理事件的change属性

Event属性和其他Trait属性不一样

 Button属性是由Event属性继承而来的

Event监听

from traits.api import HasTraits,Int,Str,Event,on_trait_change

class Child(HasTraits):
    name = Str("ZhangSan")
    age = Int(4)
    Infoupdated = Event

    #建立监听函数
    @on_trait_change("name,age")
    def Info_changed(self):
        self.Infoupdated = True #当name或者age被修改时,相应的监听函数Infoupdated将会被调用

    def _Infoupdated_fired(self):
        self.reprint()

    def reprint(self):
        print("reprint Information: %s,%s"%(self.name,self.age))

 结果测试

>>> child = Child()
>>> child.name = "Lisi"  #修改,触发事件
reprint Information: Lisi,4  
>>> child.age = 1
reprint Information: Lisi,1
>>> child.age = 1      #由于结果未改变不会触发事件
>>>
>>> child.Infoupdated = 0  #只要给该属性赋值就会触发对应的事件
reprint Information: Lisi,1
>>> child.Infoupdated = 1111
reprint Information: Lisi,1
原文地址:https://www.cnblogs.com/ssyfj/p/9308724.html