Kivy - 访问根之外的小部件 ID class
Kivy - Accessing id of widget outside of the root class
我希望检索小部件的 ID 以便访问其 text
属性,在小部件 class outside 的根 class。我理解为了影响或检索带有来自 kv-lang 的 id 的项目,可以使用以下语法:
self.ids.some_id.some_attribute = new_value
这在根目录 class 中使用时效果很好,因为它的所有子目录都可以访问。但是在小部件 class 中,class 仅引用声明的小部件,因此它之外的任何 ID 都超出范围。
<Root>:
...
SomeButton:
...
TextInput:
id: some_id
什么不起作用:
class SomeButton(Button):
def on_press(self):
print(self.ids.some_id.text)
正如我所说,这是可以理解的。但是我不知道在这种情况下使用了什么。任何帮助将不胜感激:)
问题是 ID 是规则本地的,而不是小部件。
此处您的规则是为 <Root>
声明的,因此要访问它,您必须使用对此小部件的引用,而不是对按钮的引用。
如果你想给按钮一个 some_id 的引用,你可以在你的按钮上添加一个 属性。
class SomeButton(Button):
target = ObjectProperty()
def on_press(self):
print self.target.text
和link他们一起在kv.
<Root>:
...
SomeButton:
target: some_id
TextInput:
id: some_id
我是 kivy 的新手,但我认为 TextInput 不是 SomeButton 的子窗口小部件,但您正试图从 Button 访问它。那是你的问题。
尝试self.parent.ids.some_id.text
我希望检索小部件的 ID 以便访问其 text
属性,在小部件 class outside 的根 class。我理解为了影响或检索带有来自 kv-lang 的 id 的项目,可以使用以下语法:
self.ids.some_id.some_attribute = new_value
这在根目录 class 中使用时效果很好,因为它的所有子目录都可以访问。但是在小部件 class 中,class 仅引用声明的小部件,因此它之外的任何 ID 都超出范围。
<Root>:
...
SomeButton:
...
TextInput:
id: some_id
什么不起作用:
class SomeButton(Button):
def on_press(self):
print(self.ids.some_id.text)
正如我所说,这是可以理解的。但是我不知道在这种情况下使用了什么。任何帮助将不胜感激:)
问题是 ID 是规则本地的,而不是小部件。
此处您的规则是为 <Root>
声明的,因此要访问它,您必须使用对此小部件的引用,而不是对按钮的引用。
如果你想给按钮一个 some_id 的引用,你可以在你的按钮上添加一个 属性。
class SomeButton(Button):
target = ObjectProperty()
def on_press(self):
print self.target.text
和link他们一起在kv.
<Root>:
...
SomeButton:
target: some_id
TextInput:
id: some_id
我是 kivy 的新手,但我认为 TextInput 不是 SomeButton 的子窗口小部件,但您正试图从 Button 访问它。那是你的问题。
尝试self.parent.ids.some_id.text