【问题标题】:Python Kivy: Refer to Label(Button) id (created in *.py file) via self.ids commandPython Kivy:通过 self.ids 命令引用 Label(Button) id(在 *.py 文件中创建)
【发布时间】:2017-02-07 20:03:39
【问题描述】:

我正在处理两个文件

  • test.py
  • test.kv

test.py的内容如下:

import kivy
kivy.require('1.0.0')

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
Builder.load_file("test.kv")


class MainScreen(Screen):
    def __init__(self, *args, **kwargs):
        super(MainScreen, self).__init__(*args, **kwargs)

    def printIds(self):
        print self.ids

    def printChildren(self):
        print self.ids.box.children
        for child in self.ids.box.children:
            print child.text, child.id

    def addButton(self):
        self.ids.box.add_widget(Button(text = "newly added Button", id = "test_button_id"))

    pass

class MyApp(App):
    def __init__(self,*args, **kwargs):
         super(MyApp, self).__init__(*args, **kwargs)

    def build(self):
        app = MainScreen()
        return app



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

test.kv的内容是:

<MainScreen>:
    BoxLayout:
        orientation: "horizontal"
        id: box 
        Label:
            text: "test label"
            id: test_label_id
        Button:
            text: "Print Children"
            on_release: root.printChildren()
        Button:
            text: "Print ids"
            on_release: root.printIds()
        Button:
            text: "Add Button"
            on_release: root.addButton()

我的问题如下: 我想通过 python 文件中的 self.ids 命令访问新添加的按钮(将在通过按下按钮添加按钮调用函数 addButton 时添加)。但是当我这样做时(即按下按钮打印 ids)它只显示我在 kivy 文件中分配的 ids,即:box(这是 BoxLayout 的 id)和 test_label_id(这是测试标签的 id )。

我想错了吗?我的意思是,如果我使用self.ids.box.add_widged(Button(text="...", id="...")) 命令在python 文件中添加一个按钮,它应该可以通过self.ids 命令看到,对吧?

我也可以在另一个方向上进行测试:如果我打印boxlayout 的所有孩子并抓住他们的例如使用命令child.textchild.id 的文本和ID 它会显示我新添加的按钮的文本和ID,以及其他按钮/标签的文本,但不是我在kivy 文件中分配的ID(特别是:它为他们显示无)。

我是不是做错了什么?

【问题讨论】:

    标签: python django kivy


    【解决方案1】:

    ID 仅在 kv 语言中创建,这样的操作 (Button(id='something')) 不能以常见的方式使用:

    Button:
        id: my_id
    

    会的。它不会将 id 存储在父母的 ids 字典中。但是,您也许可以通过一些巧妙的技巧来修改父级的 ids 字典(它只是一个“可观察的”字典),但最后您需要将对您的小部件的引用设置为 weak em>/proxy one,否则小部件不会被垃圾收集。

    一个更好的主意是在 kv 中为您的小部件创建一些容器,然后将其 children 属性与索引一起使用,或者创建一个自定义属性,同时循环其父子级并检查自定义属性的值,或类似的东西:

    # in App class
    class X(App):
        my_widgets = DictProperty()
    
    # in custom widget init
    def __init__(self, **kwargs):
        super(<class name>, self).__init__(**kwargs)
        app = App.get_running_app()
        app.my_widgets['my_new_widget'] = WeakProxy(Button())
    

    参考WeakProxy。 :)

    【讨论】:

    • 感谢您的快速答复!我尝试按照您的建议调整我的代码以包含字典,但执行时出现以下错误:TypeError: 'kivy.properties.DictProperty' object does not support item assignment
    • @klexx 哦,好吧,将DictProperty 移动到班级级别(删除self. 并在class MyApp... 下),然后它应该可以工作。大错特错,对不起^^检查编辑。
    • 好的,现在可以编译了。再次感谢 :) 我试图弄清楚我是否可以解决我的问题。我可能会回来... :)
    猜你喜欢
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 2016-06-22
    相关资源
    最近更新 更多