【问题标题】:How to disable a widget in Kivy?如何禁用 Kivy 中的小部件?
【发布时间】:2013-11-19 11:33:16
【问题描述】:

我阅读了 Kivy 教程,但找不到如何禁用小部件(例如,按钮)。

def foo(self, instance, *args):
  #... main business logic, and then
  instance.disable = False
  # type(instance) = kivy.uix.Button

我将foofunctools.partial 绑定。

什么是正确的参数?

【问题讨论】:

    标签: python user-interface widget kivy


    【解决方案1】:

    如果您使用的是 kivy 版本 >= 1.8,那么您只需执行 widget.disabled = True。如果在以前的版本中您可以简单地自行管理禁用,只需确保它不会对触摸做出反应并在禁用时显示另一种外观。

    【讨论】:

      【解决方案2】:
      1. disabled,不是disable
      2. 设置为真

      例子:

      from kivy.uix.button import Button
      from kivy.app import App
      from functools import partial
      
      class ButtonTestApp(App):
          def foo(self, instance, *args):
              instance.disabled = True
      
          def build(self):
              btn = Button()
              btn.bind(on_press=partial(self.foo, btn));
              return btn
      
      if __name__ == '__main__':
          ButtonTestApp().run()
      

      【讨论】:

      • 我添加了 def foo(self, instance, *args): print instance.disabled instance.disabled = True 并得到 print instance.disabled AttributeError: 'Button' object has no attribute 'disabled'
      • disabled 属性是在 1.8.0 版中引入的。如果你想使用它,你需要实现你的框架。
      • 但是怎么做呢? In the official site 写道:“当前版本是 1.7.2,于 2013 年 8 月 4 日发布。”你从哪里得到新的))?
      • 最新版本是开发版。你可以在 GitHub 项目页面上找到它:github.com/kivy/kivy
      【解决方案3】:

      在以下示例中,MyButton 遵循@qua-non 的想法。它使用BooleanProperty 来更改它的background_colorcolor。更重要的是,它在on_touch_down 中添加了一个条件if self.enabled:。如果没有on_touch_down,则没有on_touch_moveon_touch_upon_presson_release。因此,我们可以考虑禁用Button

      我使用名称 enabled 而不是 disabled 以避免未来可能出现的问题,因为使用的是 Kivy 1.8.0 的相同属性。

      from kivy.app import App
      from kivy.uix.gridlayout import GridLayout
      from kivy.properties import BooleanProperty
      from kivy.uix.button import Button
      from kivy.lang import Builder
      
      Builder.load_string("""
      <Example>:
          cols: 3
          Button:
              text: "Disable right button"
              on_press: my_button.enabled = False
          Button:
              text: "enabled right button"
              on_press: my_button.enabled = True
          MyButton:
              id: my_button
              text: "My button"
              on_press: print "It is enabled"
      """)
      
      class MyButton(Button):
          enabled = BooleanProperty(True)
      
          def on_enabled(self, instance, value):
              if value:
                  self.background_color = [1,1,1,1]
                  self.color = [1,1,1,1]
              else:
                  self.background_color = [1,1,1,.3]
                  self.color = [1,1,1,.5]
      
          def on_touch_down( self, touch ):
              if self.enabled:
                  return super(self.__class__, self).on_touch_down(touch)
      
      class Example(GridLayout):    
          pass
      
      class MyApp(App):
          def build(self):
              return Example()
      
      if __name__=="__main__":
          MyApp().run()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-30
        • 1970-01-01
        • 2021-11-29
        • 2016-11-24
        • 2022-07-12
        • 1970-01-01
        • 2015-11-16
        相关资源
        最近更新 更多