在普通Kivy中,当您在按钮上设置disabled
属性时,该按钮会自动变灰以显示状态变化。但是KivyMD的MD *按钮似乎没有这样做。我尝试绑定到disabled
并手动更改一些属性,但是如下面的示例所示,再次启用该按钮后,似乎无法恢复颜色。如何在KivyMD中显示禁用的按钮?什么是在状态更改时更改其颜色的正确方法?这种信息是否记录在某处?
# Shows two round flat buttons, "Control" and "Button". The former toggles enabled/disabled status of the latter.
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivy.properties import ObjectProperty
from kivy.lang import Builder
class MyLayout (MDBoxLayout):
control = ObjectProperty (None)
def on_press (self):
self.button.disabled = not self.button.disabled # Doesn't change color
if self.button.disabled:
self.button.color = (.4,.4,.4,.4) # Doesn't do anything
self.button.md_bg_color = (.8,.8,.8,1) # Changes background
else:
self.button.color = self.button.theme_cls.primary_color # Doesn't do anything
self.button.md_bg_color = self.button.theme_cls.primary_color # Fills the whole thing, effectively turning this into MDFillRoundFlatButton, which I don't want
# self.button.md_bg_color = (1,1,1,1) # Obliterates distinction between border and fill
self.button.bg_color = (1,1,1,1) # Doesn't do anything
class TestApp (MDApp):
def build (self):
self.theme_cls.primary_palette
return Builder.load_string ('''
MyLayout:
control: control
button: button
MDRoundFlatButton:
id: control
text: "Control"
on_press: root.on_press ()
size_hint: (.3,.3)
font_size: "50sp"
MDRoundFlatButton:
id: button
text: "Button"
md_bg_color_disabled: (.4,.4,.4,.4) # Does nothing AFAICT
size_hint: (.3,.3)
font_size: "50sp"
''')
if __name__ == '__main__':
TestApp ().run ()