我正在尝试使用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'导致此问题的原因是什么?
发布于 2015-12-22 20:44:02
在kv中管理动态小部件有点笨拙,但无论如何,问题是设置id不会设置根小部件的属性-这就是它不存在的原因。相反,您可以直接使用root.remove_widget(button_attach_process)或root.remove_widget(root.ids.button_attach_process),这也可以在python文件中使用。
https://stackoverflow.com/questions/34409244
复制相似问题