How can I bind a property to another property in 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)
在所有情况下都会调用此方法,但有时更改不会传播到子属性.
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.
推荐答案
要将一个属性绑定到另一个属性,您应该使用 setter 事件:
To bind one property to another you should use the setter event:
self.bind(_MyProperty=self._Child.setter('_MyProperty'))
这篇关于如何将一个属性绑定到 Kivy 中的另一个属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将一个属性绑定到 Kivy 中的另一个属性?


基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01