• <i id='swsKs'><tr id='swsKs'><dt id='swsKs'><q id='swsKs'><span id='swsKs'><b id='swsKs'><form id='swsKs'><ins id='swsKs'></ins><ul id='swsKs'></ul><sub id='swsKs'></sub></form><legend id='swsKs'></legend><bdo id='swsKs'><pre id='swsKs'><center id='swsKs'></center></pre></bdo></b><th id='swsKs'></th></span></q></dt></tr></i><div id='swsKs'><tfoot id='swsKs'></tfoot><dl id='swsKs'><fieldset id='swsKs'></fieldset></dl></div>
    • <bdo id='swsKs'></bdo><ul id='swsKs'></ul>

      <small id='swsKs'></small><noframes id='swsKs'>

      <tfoot id='swsKs'></tfoot>

    1. <legend id='swsKs'><style id='swsKs'><dir id='swsKs'><q id='swsKs'></q></dir></style></legend>

        如何在 Kivy 中隐藏 ActionButton?

        时间:2023-10-10

      1. <small id='5EM2C'></small><noframes id='5EM2C'>

            • <bdo id='5EM2C'></bdo><ul id='5EM2C'></ul>
              <legend id='5EM2C'><style id='5EM2C'><dir id='5EM2C'><q id='5EM2C'></q></dir></style></legend>
              <i id='5EM2C'><tr id='5EM2C'><dt id='5EM2C'><q id='5EM2C'><span id='5EM2C'><b id='5EM2C'><form id='5EM2C'><ins id='5EM2C'></ins><ul id='5EM2C'></ul><sub id='5EM2C'></sub></form><legend id='5EM2C'></legend><bdo id='5EM2C'><pre id='5EM2C'><center id='5EM2C'></center></pre></bdo></b><th id='5EM2C'></th></span></q></dt></tr></i><div id='5EM2C'><tfoot id='5EM2C'></tfoot><dl id='5EM2C'><fieldset id='5EM2C'></fieldset></dl></div>

              1. <tfoot id='5EM2C'></tfoot>
                    <tbody id='5EM2C'></tbody>

                  本文介绍了如何在 Kivy 中隐藏 ActionButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试根据当前屏幕(使用屏幕管理器)修改 ActionButton 的可见性.我找不到可以简单地切换可见性的 Visible 属性或类似的东西(对于 ActionButton 和一般的小部件都不是).

                  I am trying to modify the visibility of an ActionButton accordingly to the current screen (using Screen Manager). I could not find a Visible property or something like that that can simply toggle visibility (neither for ActionButton nor for Widgets in general) .

                  2013 年的一篇帖子 建议更改按钮,但我不想依靠这样的 hack 来完成这样一个简单的任务,而且我的应用程序的背景会是可变的.

                  A post from 2013 suggests changing the texture of the button, but I don't want to rely on such a hack to accomplish such a simple task, besides, the background of my app will be variable.

                  另一篇文章建议删除小部件并根据需要再次添加.尽管它不必要的复杂性.我修改为适用于我的情况(ActionBar 和 ActionButton),所以我从 ActionView 中清除了小部件,然后尝试添加 ActionButton.我尝试同时存储弱引用和 self 成员,但都出现以下错误:

                  Another post suggest removing the widget and adding it again as needed. Despite its unnecessary complexity. I modified to work in my case (ActionBar and ActionButton), so I clear the widgets from the ActionView and then try to add the ActionButton. I tried storing both a weakref and the self member, with both I got the following error:

                  WidgetException: Cannot add <kivy.uix.actionbar.ActionButton object at 0x7fcd3fe22ce8>, it already has a parent <kivy.uix.actionbar.ActionView object at 0x7fcd3fe22870>
                  

                  任何想法将不胜感激.我正在使用开发版本,但它不适用于 1.8.

                  Any idea would be greatly appreciated. I am working with the dev version, but it neither work with 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()
                  

                  该函数在场景加载后运行:

                  This function is run after the scene is loaded:

                  def _initialize(self):
                    self.next = self.ids.btn_next.__self__ # same result if I don't use .__self__
                  

                  此代码引发了上面发布的异常:

                  This code raises the exception posted above:

                  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>
                  

                  推荐答案

                  如果目标是隐藏它们,从父级移除小部件不是一个好主意.当从 Kivy 树结构中删除某些内容时,它会被垃圾收集器拾取.您可以尝试保留对它的引用,但从概念上讲隐藏不同于删除.

                  Removing widgets from parents is not a very good idea, if the goal is to hide them. When something is removed from the Kivy tree structure, it will be picked up by the garbage collector. You could try to keep a reference to it, but conceptually hidding is different from removing.

                  对我来说最好的解决方案是使用 opacity 属性.它与可见属性的原理类似,但更强大,因为它接受渐变(和动画).

                  The best solution for me has been using the opacity property. It is kind of the same principle of a visible property but more powerful because it accepts gradients (and animations).

                  警告"是 您必须考虑到 Widget 仍然存在.它只是看不见的.在某些情况下,您可能想尝试将 opacitydisabled 属性结合使用.

                  The "caveat" is that you have to consider that the Widget is still there. It is just invisible. In some cases, you might want to try and combine the opacity with the disabled property.

                  Button:
                      opacity: 0
                      disabled: True # To make sure it doesn't capture events accidentally
                  

                  这篇关于如何在 Kivy 中隐藏 ActionButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:ObjectProperty 类的使用 下一篇:如何在 kv 语言中动态制作很多按钮?

                  相关文章

                  最新文章

                    1. <tfoot id='Humyf'></tfoot>

                      <small id='Humyf'></small><noframes id='Humyf'>

                    2. <legend id='Humyf'><style id='Humyf'><dir id='Humyf'><q id='Humyf'></q></dir></style></legend>
                      <i id='Humyf'><tr id='Humyf'><dt id='Humyf'><q id='Humyf'><span id='Humyf'><b id='Humyf'><form id='Humyf'><ins id='Humyf'></ins><ul id='Humyf'></ul><sub id='Humyf'></sub></form><legend id='Humyf'></legend><bdo id='Humyf'><pre id='Humyf'><center id='Humyf'></center></pre></bdo></b><th id='Humyf'></th></span></q></dt></tr></i><div id='Humyf'><tfoot id='Humyf'></tfoot><dl id='Humyf'><fieldset id='Humyf'></fieldset></dl></div>

                        <bdo id='Humyf'></bdo><ul id='Humyf'></ul>