12

I read the Kivy tutorial and couldn't find how to disable a widget (for example, a Button).

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

I bind foo with functools.partial.

What is the correct parameter?

Michael Currie
  • 13,721
  • 9
  • 42
  • 58
Ulrich Von Rekkenin
  • 352
  • 1
  • 3
  • 14

3 Answers3

19

If you are using kivy version >= 1.8 then you can just do widget.disabled = True. If on previous versions you can simply manage the disabling yourself, just make sure it doesn't react to touch and displays a alternative look when disabled.

qua-non
  • 4,152
  • 1
  • 21
  • 25
18
  1. It's disabled, not disable
  2. Set it to True

Example:

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()
Nykakin
  • 8,657
  • 2
  • 29
  • 42
  • I added `def foo(self, instance, *args): print instance.disabled instance.disabled = True` and got _print instance.disabled AttributeError: 'Button' object has no attribute 'disabled'_ – Ulrich Von Rekkenin Nov 07 '13 at 18:11
  • `disabled` property was introduced in version 1.8.0. If you want to use it you need to actualize your framework. – Nykakin Nov 07 '13 at 19:33
  • But how? [In the official site](http://kivy.org/#download) was wrote that "The current version is 1.7.2, released on Aug 4th, 2013." Where did you get the new one))? – Ulrich Von Rekkenin Nov 07 '13 at 21:46
  • Newest version is a dev one. You can find it on GitHub project page: https://github.com/kivy/kivy – Nykakin Nov 07 '13 at 22:54
7

In the following example MyButton follows @qua-non idea. It uses a BooleanProperty to change the background_color and color of it. More important, it adds a condition if self.enabled: in the on_touch_down. If there is no on_touch_down, then there is no on_touch_move, on_touch_up, on_press or on_release. Therefore, we can consider the Button disabled.

I use the name enabled instead of disabled to avoid possible future problems with by using the same attribute of 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()
toto_tico
  • 17,977
  • 9
  • 97
  • 116