<p>在下面的示例中,<code>MyButton</code>遵循@qua non idea。它使用<code>BooleanProperty</code>来改变它的<code>background_color</code>和<code>color</code>。更重要的是,它在<code>on_touch_down</code>中添加了一个条件<code>if self.enabled:</code>。如果没有<code>on_touch_down</code>,那么就没有<code>on_touch_move</code>、<code>on_touch_up</code>、<code>on_press</code>或<code>on_release</code>。因此,我们可以考虑禁用<code>Button</code>。</p>
<p>我使用名称<code>enabled</code>,而不是<code>disabled</code>,通过使用Kivy 1.8.0的相同属性来避免将来可能出现的问题。</p>
<pre><code>from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.properties import BooleanProperty
from kivy.uix.button import Button
from kivy.lang import Builder
Builder.load_string("""
<Example>:
cols: 3
Button:
text: "Disable right button"
on_press: my_button.enabled = False
Button:
text: "enabled right button"
on_press: my_button.enabled = True
MyButton:
id: my_button
text: "My button"
on_press: print "It is enabled"
""")
class MyButton(Button):
enabled = BooleanProperty(True)
def on_enabled(self, instance, value):
if value:
self.background_color = [1,1,1,1]
self.color = [1,1,1,1]
else:
self.background_color = [1,1,1,.3]
self.color = [1,1,1,.5]
def on_touch_down( self, touch ):
if self.enabled:
return super(self.__class__, self).on_touch_down(touch)
class Example(GridLayout):
pass
class MyApp(App):
def build(self):
return Example()
if __name__=="__main__":
MyApp().run()
</code></pre>