[英]How do I have a checkbox enable/disable a button using Kivy?
如果選中該復選框,我希望啟用該按鈕。 如果未選中,我希望將其禁用。 我認為這就是我的disable_button
function 通過檢查復選框是否被選中if self.ids.checkbox_confirm.active == False:
然后禁用按鈕self.ids.submit_button.disabled == True
的。 但是,后一種說法沒有做任何事情。
主程序
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.