我试图禁用Kivy按钮。为此,我创建了一个BooleanProperty,并将禁用的按钮属性设置为BooleanProperty。当我启动时,按钮被正确创建,其值已经分配(False)。但是,当我更新BooleanProperty时,按钮没有任何反应。当我点击时,我知道属性被正确绑定和更改,因为打印功能显示它已被更改。
可能是什么问题?我正在使用Kivy 1.10.0。我只保留了相关的代码行 - 我不认为我错过了什么,但我添加了关于什么有用的评论。
class LoginScreen(GridLayout):
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.selection = SelectionScreen()
self.add_widget(self.selection)
self.selection.bind(buttonStatus=self.disableButton)
def disableButton(self, instance, value):
self.selection.buttonStatus = value
print(self.selection.buttonStatus) # returns True when I press button
class SelectionScreen(GridLayout):
buttonStatus = BooleanProperty()
# button to disable
self.buttonStatus = False # putting True disables button at launch
btn = Button(text='BTN',disabled=self.buttonStatus)
btn.bind(on_press=self.btnFunction)
self.add_widget(btn)
def btnFunction(self, value=None):
self.buttonStatus = True # this updates property but interface not affected
答案 0 :(得分:0)
btn = Button(text='BTN', disabled=self.buttonStatus)
不会将buttonStatus
属性与按钮的disabled
属性绑定。为此,您可以将bind
与setter
方法一起使用:
self.bind(buttonStatus=btn.setter("disabled"))
基于您的代码的示例:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.properties import BooleanProperty
from kivy.uix.button import Button
class MainWindow(GridLayout):
buttonStatus = BooleanProperty()
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.cols = 1
self.buttonStatus = False
btn = Button(text='BTN', disabled=self.buttonStatus)
btn.bind(on_press=self.btnFunction)
self.bind(buttonStatus=btn.setter("disabled"))
self.add_widget(btn)
def btnFunction(self, *args):
self.buttonStatus = True
class TestApp(App):
def build(self):
return MainWindow()
if __name__ == '__main__':
TestApp().run()