2016-08-21 7 views
0

私はアンドロイドロックのようなものを作りたいので、ボタンの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

関連する問題