如何使用 Kivy 使复选框 enable/disable 成为按钮?

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

如果选中该复选框,我希望启用该按钮。如果未选中,我希望将其禁用。我认为这就是我的 disable_button 函数通过检查复选框是否被选中 if self.ids.checkbox_confirm.active == False: 然后使用 self.ids.submit_button.disabled == True 禁用按钮所做的事情。但是,后面的语句没有做任何事情。

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 文件

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"

我没有测试,但你的代码有错别字。

要分配新值,您必须使用 = 而不是 ==

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 `==`

顺便说一句:

你可以用not

单行写
self.ids.submit_button.disabled = not self.ids.checkbox_confirm.active