Reputation: 179
I have a checkbox in my .kv file, and I want to disable a text input associated with it when the box is not checked.
My .py Code
if self.ids.checkbox.active==False:
self.ids.input.disabled==True
else:
self.ids.input.disabled==False
My .kv Code
TextInput:
id: input
text: "2fbd3320a168d5c2e35"
pos_hint: {"x":0.1, "y":0.4}
size_hint: 0.5,0.05
background_disabled_normal: ""
CheckBox:
id: checkbox
pos_hint: {"x":0.6, "y":0.4}
size_hint: 0.05,0.05
I do not know how to go about doing this, the .py code must be in a function, but checkboxes do not harbor the on_release attribute.
Upvotes: 1
Views: 2388
Reputation: 4513
You need to associate the event (checkbox.active
) with the corresponding callback. You can do it in the .py file using bind
:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
kv_text = """\
<MyWidget>
TextInput:
id: input
text: "2fbd3320a168d5c2e35"
pos_hint: {"x":0.1, "y":0.4}
size_hint: 0.5,0.05
background_disabled_normal: ""
disabled: True
CheckBox:
id: checkbox
pos_hint: {"x":0.6, "y":0.4}
size_hint: 0.05,0.05
"""
class MyWidget(FloatLayout):
def __init__(self, **kwargs):
super(MyWidget, self).__init__(**kwargs)
self.ids.checkbox.bind(active=self.disable_input)
def disable_input(self, checkbox, checked):
self.ids.input.disabled = not checked
class MyWidgetApp(App):
def build(self):
return MyWidget()
def main():
Builder.load_string(kv_text)
app = MyWidgetApp()
app.run()
if __name__ == '__main__':
main()
Another option is to do it in the .kv file:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
kv_text = """\
<MyWidget>
TextInput:
id: input
text: "2fbd3320a168d5c2e35"
pos_hint: {"x":0.1, "y":0.4}
size_hint: 0.5,0.05
background_disabled_normal: ""
disabled: not checkbox.active # <<<<<<<<<
CheckBox:
id: checkbox
pos_hint: {"x":0.6, "y":0.4}
size_hint: 0.05,0.05
"""
class MyWidget(FloatLayout):
pass
class MyWidgetApp(App):
def build(self):
return MyWidget()
def main():
Builder.load_string(kv_text)
app = MyWidgetApp()
app.run()
if __name__ == '__main__':
main()
Upvotes: 4