Python-Kivy:为特定儿童定义的on_touch_down()会影响所有儿童

时间:2016-08-21 08:27:19

标签: python kivy kivy-language

我想制作类似android的锁,所以我有2个按钮图像(按钮正常,按下按钮)。

我在每个图像上定义了一个函数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

1 个答案:

答案 0 :(得分:0)

不是定义# create console handler and set level to debug ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) # add ch to logger logger.addHandler(ch) logger.debug('debug sample message') 事件处理程序,而是在on_touch_*的帮助下定义可点击的图像类:

ButtonBehavior

现在,您可以在kv文件中使用它。还有其他行为,您可以查看here