如果選中該復選框,我希望啟用該按鈕。如果未選中,我希望將其禁用。我認為這就是我的disable_button功能通過檢查復選框是否被選中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"
uj5u.com熱心網友回復:
我沒有測驗它,但你的代碼有錯字。
要分配新值,您必須使用=而不是==
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/441343.html
上一篇:撰寫一個名為first_word的函式,它回傳字串中的第一個單詞
下一篇:修改laravel中的默認登錄
