Python-Kivy:on_touch_down()定义给特定的孩子影响所有儿童

问题描述:

我想做一个类似于android-lock的东西,所以我有2个按钮图像(按钮正常,按下按钮)。Python-Kivy:on_touch_down()定义给特定的孩子影响所有儿童

我在每个图像上定义了一个函数on_touch_down,所以当我按下它时,它将源更改为按下按钮,on_touch_up将其更改回正常。但每次按下屏幕的任何部分时,它都会一次更改所有按钮。

当我按下按钮时,如何才能使每个按钮都更改,而当我按下任何按钮时不会更改所有按钮?

这里是我的KV文件:

Manager: 
    Principal: 

<Principal>: 
    GridLayout: 
     cols: 3 
     Image: 
      id: '1' 
      size: 30,30 
      source: 'button.png' 
      on_touch_down: self.source = 'button_press.png' 
      on_touch_up: self.source = 'button.png' 
      allow_strech: True 
     Image: 
      id: '2' 
      size: 30,30 
      source: 'button.png' 
      on_touch_down: self.source = 'button_press.png' 
      on_touch_up: self.source = 'button.png' 
      allow_strech: True 
     Image: 
      id: '3' 
      size: 30,30 
      source: 'button.png' 
      on_touch_down: self.source = 'button_press.png' 
      on_touch_up: self.source = 'button.png' 
      allow_strech: True 
     Image: 
      id: '4' 
      size: 30,30 
      source: 'button.png' 
      on_touch_down: self.source = 'button_press.png' 
      on_touch_up: self.source = 'button.png' 
      allow_strech: True 
     Image: 
      id: '5' 
      size: 30,30 
      source: 'button.png' 
      on_touch_down: self.source = 'button_press.png' 
      on_touch_up: self.source = 'button.png' 
      allow_strech: True 
     Image: 
      id: '6' 
      size: 30,30 
      source: 'button.png' 
      on_touch_down: self.source = 'button_press.png' 
      on_touch_up: self.source = 'button.png' 
      allow_strech: True 
     Image: 
      id: '7' 
      size: 30,30 
      source: 'button.png' 
      on_touch_down: self.source = 'button_press.png' 
      on_touch_up: self.source = 'button.png' 
      allow_strech: True 
     Image: 
      id: '8' 
      size: 30,30 
      source: 'button.png' 
      on_touch_down: self.source = 'button_press.png' 
      on_touch_up: self.source = 'button.png' 
      allow_strech: True 
     Image: 
      id: '9' 
      size: 30,30 
      source: 'button.png' 
      on_touch_down: self.source = 'button_press.png' 
      on_touch_up: self.source = 'button.png' 
      allow_strech: True 

而不是定义on_touch_*事件处理程序,定义了一个可点击的图片类的ButtonBehavior的帮助:

from kivy.uix.behaviors.button import ButtonBehavior 

class ClickableImage(ButtonBehavior, Image): 

    def on_press(self): 
     pass 

    def on_release(self): 
     pass 

现在,你可以使用它你kv文件。还有其他的行为,你可以检查他们here

+0

这解决了我的问题!谢谢 – gramsch