【问题标题】:Kivy: How to get the id of an object from itself?Kivy:如何从自身获取对象的 id?
【发布时间】:2021-03-15 13:12:13
【问题描述】:

我在 kivy 中使用了很多 ids。我想从我的对象本身获取id,但我不知道如何正确编写它。 (self.id 不起作用,它返回None

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout

KV = '''
BoxLayout:
    ObjBoxLayout:
        id: my_box
'''

class MyApp(App):
   def build(self):
       box = Builder.load_string(KV)
       print(box.ids.keys())
       return box


class ObjBoxLayout(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        print(self.id)

if __name__ == "__main__":
   MyApp().run()

【问题讨论】:

    标签: python kivy ids


    【解决方案1】:

    没有直接的方法可以做到这一点,尽管从技术上讲,您可以遍历根的 id 以找到匹配的。

    如果您想要这种功能,最好添加您自己设置和访问的标识属性。

    【讨论】:

    • 我将以自定义方式进行操作,谢谢您的回答。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2012-04-23
    相关资源
    最近更新 更多