如何在Kivy中将一个属性绑定到另一个属性?

如何在Kivy中将一个属性绑定到另一个属性?

问题描述:

我想将小部件的属性绑定到子小部件的属性.因此,当根窗口小部件属性更改时,更改也将传播到子属性.

I want to bind a property of a widget to a property of a child widget. Thus when the root widget property is changed, the change is propagated to the child property as well.

我这样尝试过:

self._Child._MyProperty = self._MyProperty

这可行...有时.但有时它不起作用.我找不到它什么时候起作用,为什么以及在什么情况下不起作用.

This works... sometimes. But sometimes it does not work. I cannot find out when it works or why and under which conditions it does not work.

在所有情况下,我也都绑定到根窗口小部件中的方法:

In all cases I have a binding to a method in the root widget as well:

self.bind(_MyPropert = self._MyPropertyChange)

在所有情况下都将调用此方法,但有时更改不会传播到child属性.

This method is called in all cases, but sometimes the change is not propagated to the child property.

即使感觉很自然,这也不起作用:

This does not work even if it feels very natural:

self.bind(_MyProperty = self._Child._MyProperty)

但是在Kivy中,我可以这样做:

But in Kivy, I could do:

<RootWidget>
    <ChildWidget>
        _MyProperty: self.parent._MyProperty

问题是我想用Python而不是Kivy来实现.

The problem is I want to do it in Python, not in Kivy.

要将一个属性绑定到另一个属性,您应该使用

To bind one property to another you should use the setter event:

self.bind(_MyProperty=self._Child.setter('_MyProperty'))