如何从Python代码文件中设置Kivy小部件ID [英] How to set kivy widget id from Python code file

查看:94
本文介绍了如何从Python代码文件中设置Kivy小部件ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将ID分配给通过python函数创建的新kivy小部件的帮助

I need help assigning ids to new kivy widgets that are created from a python function

我已经尝试过:

old = Label(id = 'old')

和:

old = Label()
old.id = 'old'

但是它似乎不起作用,因为每当我尝试引用这些小部件时,它都会给我一个错误

but it doesn't seem to work because, whenever i try referencing the widgets, it gives me an error

推荐答案

在Python代码中创建id的方法是正确的.

The ways that you have creating id in Python code are correct.

但是您不能使用self.ids.oldself.ids['old']引用它们,因为它们在self.ids中不存在. self.ids字典类型属性仅包含所有用kv文件中定义的ID标记的小部件.

But you cannot reference them using self.ids.old or self.ids['old'] because they don't exist in self.ids. The self.ids dictionary type property contains only all widgets tagged with ids defined inside kv file.

要引用Python代码中定义的id,在此示例中,请使用self.old.

To reference id defined in Python code, in this example use self.old.

当解析您的kv文件时,kivy会收集所有标有的小部件 id,并将其放在此self.ids字典类型属性中.那 意味着您还可以遍历这些小部件并访问它们 字典样式.

When your kv file is parsed, kivy collects all the widgets tagged with id’s and places them in this self.ids dictionary type property. That means you can also iterate over these widgets and access them dictionary style.

这篇关于如何从Python代码文件中设置Kivy小部件ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆