Weeoowee
Weeoowee

Reputation: 25

How do I have a checkbox enable/disable a button using Kivy?

If the checkbox is checked, I want the button to be enabled. If it isn't checked, I want it to be disabled. I thought that is what my disable_button function does by checking if the checkbox is checked if self.ids.checkbox_confirm.active == False: and then disabling the button with self.ids.submit_button.disabled == True. But, the latter statement isn't doing anything.

main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.properties import ObjectProperty


class MainWindow(Screen):
    def on_pre_enter(self):
        Window.size=(750,400)

    def checkbox_click(self, instance, value):
        return value

    def disable_button(self):
        print(self.ids.checkbox_confirm.active)
        if self.ids.checkbox_confirm.active == False:
            self.ids.submit_button.disabled == True
        else:
            self.ids.submit_button.disabled == False


class SecondWindow(Screen):
    def on_pre_enter(self):
        Window.fullscreen='auto'
    pass

class WindowManager(ScreenManager):
    pass

class MyMainApp(App):
    def build(self):
        Window.clearcolor = (1,1,1,1)
        return kv

kv = Builder.load_file("my.kv")

if __name__ == "__main__":
    MyMainApp().run()

.kv file

WindowManager:
    MainWindow:
    SecondWindow:

<MainWindow>:
    name: "main"

    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height
        padding: 50

        Label:
            text: "Email"
            color: 0,0,0,1
            font_size: 32

        BoxLayout:
            orientation: "horizontal"
            Label:
                text: "Email Address:"
                color: 0,0,0,1

            TextInput:
                size_hint_y: None
                pos_hint: {'center_y': .5}
                height: 38
                multiline: True
                padding: 10
        BoxLayout:
            orientation: "horizontal"

            Label:
                text: "I double-checked that my email is typed correctly:"
                color: 0,0,0,1
            CheckBox:
                id: checkbox_confirm
                on_active:
                    root.checkbox_click(self, self.active)
                    root.disable_button()
                pos_hint: {'center_x': .5}


        BoxLayout
            orientation: "vertical"

            Button:
                id:submit_button
                text: "Submit"

                size_hint: (0.2, None)
                pos_hint: {'center_x': .5}
                height: 50


                on_release:
                    app.root.current = "second"
                    root.manager.transition.direction = "left"

<SecondWindow>:
    name: "second"

Upvotes: 1

Views: 729

Answers (1)

furas
furas

Reputation: 142681

I didn't test it but you have typo in code.

To assign new value you have to use = instead of ==

if self.ids.checkbox_confirm.active == False:
    self.ids.submit_button.disabled = True   # need `=` instead of `==`
else:
    self.ids.submit_button.disabled = False  # need `=` instead of `==`

BTW:

You can write it in single line using not

self.ids.submit_button.disabled = not self.ids.checkbox_confirm.active

Upvotes: 1

Related Questions