【问题标题】:Kivy [widget] has no attribute [child_widget]Kivy [widget] 没有属性 [child_widget]
【发布时间】:2015-12-22 05:48:28
【问题描述】:

我正在尝试使用remove_widget 功能使我的按钮在单击后消失。根据documentation,我相信这是实现这一目标的正确方法。但是,当我尝试删除按钮时,我遇到了崩溃。不确定这是否与小部件的引用类型或其他内容有关。

这是我的 main.kv

<MainPanel>:
orientation: 'vertical'
spacing: 1

AppActionBar:
    size_hint: (1., 0.1)

ScrollView:
    id: scrollview_main
    do_scroll_x: False
    do_scroll_y: False if root.fullscreen else (content.height > root.height - dp(16))
    AnchorLayout:
        id: anchorlayout_main
        size_hint_y: None
        height: root.height if root.fullscreen else max(root.height, content.height)
        GridLayout:
            id: content
            cols: 1
            spacing: '8dp'
            padding: '8dp'
            size_hint: (1, 1) if root.fullscreen else (.8, None)
            height: self.height if root.fullscreen else self.minimum_height
            Button:
                id: button_open_process
                size_hint_y: None
                text: 'Open New Process'
                height: '48dp'
                width: '120dp'
                on_release:
                    root.open_process()
                    root.remove_widget(root.button_attach_process) <-- offending line
                    #root.remove_widget(root.button_open_process)
            Button:
                id: button_attach_process
                size_hint_y: None
                text: 'Attach to Currently Running Process'
                height: '48dp'
                width: '120dp'
                on_release: root.attach_process()

单击 ID 为 button_open_process 的按钮时出现的错误

AttributeError: 'MainPanel' object has no attribute 'button_attach_process'

是什么导致了这个问题?

【问题讨论】:

    标签: kivy kivy-language


    【解决方案1】:

    在 kv 中管理动态小部件有点尴尬,但无论如何,问题是设置 id 不会设置根小部件的属性 - 这就是它不存在的原因。你可以直接使用root.remove_widget(button_attach_process),或者root.remove_widget(root.ids.button_attach_process),这也可以在python文件中使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-07
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 2021-05-29
      • 1970-01-01
      相关资源
      最近更新 更多