这是我的简化代码:
class Player(Widget):
_health = NumericProperty(100)
def _get_health(self):
return self._health
health = AliasProperty(_get_health, bind=['_health'])
我这样做是为了使health
“只读”。
奇怪的是,当我移除时,它不起作用bind=['_health']
。
所以我的问题是:bind=['_health']
实际上是做什么的?我了解AliasProperty
允许我定义在访问属性时运行的getter和可选setter(类似于Python的@property
)。
那么bind=['_health']
在这种情况下_health
该怎么办?为什么排除该属性后该属性不起作用(不反映底层的状态)?
该bind
参数告诉属性引擎哪些其他属性需要监视更改,因为它们与所AliasProperty
考虑的内容相关。
让我们以文档中的(简化)示例为例:
def get_right(self):
return self.x + self.width
right = AliasProperty(get_right, bind=['x', 'width'])
在这里,right
依赖于其他两个属性。如果只写了bind=['x']
,则仅更改为x
会导致触发的更改事件right
,而更改width
则将被忽略(但是,如果width
更改x
发生后,将触发具有预期值的事件,并且如果该值是手动检索的,也将是正确的)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句