2016-08-21 148 views
0

我想做一個類似於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 

回答

0

而不是定義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