在Kivy lang和python中访问子窗口小部件属性的值 [英] Accessing value of child widget property in kivy lang and python
问题描述
-
如何覆盖Kivy小部件的子项的默认值?即
MyWidget.label
是'default'但我想将其更改为MyRootWidget
的孩子时是紫色乌龟"?
How can I overwrite the default value of a Kivy widget's child? i.e.
MyWidget.label
is 'default' but I want to change it to e.g. 'purple turtle' when a child ofMyRootWidget
?
我可以像在MyRootWidget.__init__()
中那样访问孩子的孩子,但是这似乎很麻烦,尤其是对于一棵深树……是否有更优雅的方法呢?
I can access the children of children as I've done in MyRootWidget.__init__()
, but it seems cumbersome, especially for a deep tree ... is there a more elegant way of doing it?
我一直在查看Kivy lang和Widget页面,但如果有解决方案,则无法理解.我在SO页面中还没有看到过这个问题(尽管我在搜索时确实回答了一个不同的问题).
I've been looking over the Kivy lang and Widget pages, but haven't been able to understand a solution if it's there. I haven't seen quite this issue in SO pages (though the did answer a different question while I was searching).
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty, StringProperty
root = Builder.load_string('''
<MyWidget@BoxLayout>:
orientation: 'vertical'
Label:
id: label
text: 'DEFAULT'
<MyRootWidget@BoxLayout>:
id: root_widget
MyWidget:
id: w1
# (---1---)
''')
class MyRootWidget(BoxLayout):
w1 = ObjectProperty()
def __init__(self, **kwargs):
super().__init__(**kwargs)
print(self.ids.w1.ids.label.text) # (---2---)
class MainApp(App):
def build(self):
return MyRootWidget()
if __name__ == '__main__':
MainApp().run()
推荐答案
首先,在实现任何代码之前,必须设计您的类.
First of all, before implementing any code, you must design your classes.
首先,我们将使用MyWidget进行处理,在您的要求中,您指示您希望文本是可修改的,因此它必须是根属性.
First we will do it with MyWidget, in your requirements you indicate that you want the text to be modifiable so it must be a root property.
MyWidget
┌--------------------------┐
| |
| ┌-------------┐ text---┼--->
| | Label | | |
| | *text ---┼-----┘ |
| └-------------┘ |
└--------------------------┘
使用MyRootWidget也可以这样做:
The same could be done with MyRootWidget:
MyRootWidget
┌-----------------------------┐
| |
| ┌-------------┐ obj_widget--┼--->
| | MyWidget --┼-----┘ |
| | | |
| └-------------┘ |
└-----------------------------┘
属性的深度取决于您的要求,但是如果我们观察到公开一个属性,则意味着在根中创建一个属性并进行绑定,以便在修改根属性时也可以修改内部属性.
The depth of the property depends on your requirements, but if we observe exposing a property it implies creating a property in the root and making a binding so that when the root property is modified the internal property is modified as well.
实施以上操作,您将获得以下内容:
Implementing the above you get the following:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty, StringProperty
root = Builder.load_string('''
<MyWidget@BoxLayout>:
text: "DEFAULT"
obj_label: label
orientation: 'vertical'
Label:
id: label
text: root.text
<MyRootWidget@BoxLayout>:
obj_widget: w1
MyWidget:
id: w1
text: "purple turtle"
''')
class MyRootWidget(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
print(self.obj_widget.text)
class MainApp(App):
def build(self):
return MyRootWidget()
if __name__ == '__main__':
MainApp().run()
因此,要避免使用id,您可以像创建wid的obj_widget一样为子代小部件创建别名.
So to avoid the ids you can create an alias to the children widget like I did with obj_widget which is an alias of w1.
根据设计,您不应直接访问完整的树,而必须修改图层的属性,如果该图层被修改,则必须在其内部更新必需的数据,以免避免类之间的耦合.
By design you should not access the complete tree directly but you must modify the property of a layer and this layer if it is modified then you must update the necessary data in its internal part so we avoid the coupling between classes.
这篇关于在Kivy lang和python中访问子窗口小部件属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!