我试图根据当前屏幕(使用屏幕管理器)修改ActionButton的可见性。我无法找到一个Visible属性或类似于此的东西,可以简单地切换可见性(对于ActionButton或一般小部件都没有)。2013年的一篇文章建议更改按钮的纹理,但我不想依靠这样的技巧来完成如此简单的任务,而且我的应用程序背景将是可变的。另一篇文章建议根据需要删除小部件并再次添加它。尽管它不必要地复杂,但我修改后适用于我的情况(ActionBar和ActionButton),所以我清除了ActionView中的小部件,然后尝试添加ActionButton。我尝试存储弱引用和self成员,但两者都会出现以下错误:
任何想法都将不胜感激。我正在使用开发版本,但它既不能与1.8配合使用。
编辑 我尝试了以下代码:
WidgetException: Cannot add <kivy.uix.actionbar.ActionButton object at 0x7fcd3fe22ce8>, it already has a parent <kivy.uix.actionbar.ActionView object at 0x7fcd3fe22870>
任何想法都将不胜感激。我正在使用开发版本,但它既不能与1.8配合使用。
编辑 我尝试了以下代码:
<AppActionBar>:
ActionView:
id: av
ActionButton:
id: btn_next
text: 'Next screen'
icon: 'data/icons/next_dark.png'
important: True
on_release: app.go_next()
这个函数在场景加载后运行:
def _initialize(self):
self.next = self.ids.btn_next.__self__ # same result if I don't use .__self__
这段代码会引发上面提到的异常:
self.ids.av.clear_widgets()
self.ids.av.add_widget(self.next)
这是完整的异常跟踪:
self._mainloop()
File "/usr/local/lib/python2.7/dist-packages/kivy/core/window/window_pygame.py", line 266, in _mainloop
EventLoop.idle()
File "/usr/local/lib/python2.7/dist-packages/kivy/base.py", line 330, in idle
Clock.tick_draw()
File "/usr/local/lib/python2.7/dist-packages/kivy/clock.py", line 429, in tick_draw
self._process_events_before_frame()
File "/usr/local/lib/python2.7/dist-packages/kivy/clock.py", line 562, in _process_events_before_frame
if event.tick(self._last_tick) is False:
File "/usr/local/lib/python2.7/dist-packages/kivy/clock.py", line 309, in tick
ret = callback(self._dt)
File "/usr/local/lib/python2.7/dist-packages/kivy/uix/boxlayout.py", line 174, in do_layout
c.width = w
File "properties.pyx", line 345, in kivy.properties.Property.__set__ (kivy/properties.c:3589)
File "properties.pyx", line 377, in kivy.properties.Property.set (kivy/properties.c:4064)
File "properties.pyx", line 431, in kivy.properties.Property.dispatch (kivy/properties.c:4657)
File "/usr/local/lib/python2.7/dist-packages/kivy/uix/actionbar.py", line 552, in on_width
self._layout_all()
File "/usr/local/lib/python2.7/dist-packages/kivy/uix/actionbar.py", line 441, in _layout_all
super_add(child)
File "/usr/local/lib/python2.7/dist-packages/kivy/uix/boxlayout.py", line 212, in add_widget
return super(BoxLayout, self).add_widget(widget, index)
File "/usr/local/lib/python2.7/dist-packages/kivy/uix/layout.py", line 78, in add_widget
return super(Layout, self).add_widget(widget, index)
File "/usr/local/lib/python2.7/dist-packages/kivy/uix/widget.py", line 466, in add_widget
% (widget, parent))
WidgetException: Cannot add <kivy.uix.actionbar.ActionButton object at 0x7fecb5d6ed50>, it already has a parent <kivy.uix.actionbar.ActionView object at 0x7fecb5d6e8d8>
self.next.opacity = 0
,并且通过self.next.opacity = 1
来显示它,虽然这可能不完全是您想要的,因为它只会使其变得透明。 - martin