How to disable a widget in Kivy?

I read the Kivy tutorial and could not find how to disable the widget (e.g. button).

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

I associate foowith functools.partial.

What is the correct option?

+4
source share
3 answers

If you are using kivy version> = 1.8, you can just do widget.disabled = True. If in previous versions you can simply control the disconnection of yourself, just make sure that it does not respond to touch and displays an alternative view when disconnected.

+13
source
  • This disabled, notdisable
  • Set 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()
+10
source

MyButton @qua-non idea. BooleanProperty background_color color. , if self.enabled: on_touch_down. on_touch_down, on_touch_move, on_touch_up, on_press on_release. Button.

I use the name enabledinstead disabledto avoid possible future problems using the same Kivy 1.8.0 attribute.

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()
+4
source