简体   繁体   English

kivy:如何使图像像按钮一样

[英]kivy: how to make an image act like a button

I'm new to Kivy. 我是Kivy的新手。 I want an image to change into a different one when it is pressed (touched). 我希望在按下(触摸)图像时将其更改为其他图像。 There should be some easy way of doing this but I can't figur out what. 应该有一些简单的方法可以做到这一点,但我不知道该怎么办。

<Game>
   Image:
     id: "11"
     center_x: root.width / 2 - root.height * 1/5 * 1.5 
     center_y: root.height * 1/5
     size: root.height / 6, root.height / 6
     source: root.images[0]

This is part of my code. 这是我的代码的一部分。 I don't know how to make my image 'pressable'. 我不知道如何使我的图像“可压缩”。 Does it have to be a button of some sort? 它一定是某种按钮吗?

I believe the best way you could do this, is using a dynamic class created on the fly in your kv file http://kivy.org/docs/api-kivy.lang.html#dynamic-classes 我相信最好的方法是在kv文件http://kivy.org/docs/api-kivy.lang.html#dynamic-classes中动态创建动态类

<ImageButton@ButtonBehavior+Image>:
     on_press: self.parent.callback()
# this class is created on the fly, named ImageButton and subclasses ButtonBehavior and Image
# It's basically an Image that can act like a Button.

<Game>
   ImageButton:
     id: "11"
     center_x: root.width / 2 - root.height * 1/5 * 1.5 
     center_y: root.height * 1/5
     size: root.height / 6, root.height / 6
     source: root.images[0]

Where callback() is a method in Game that changes the source of the image. 其中callback()Game中更改图像来源的方法。

Inside Game class: 内部Game类:

def callback(self, instance, value):
    self.ids['11'].source = new_source
# or you could switch sources each click for instance

or something similar(look here: http://kivy.org/docs/api-kivy.uix.widget.html#kivy.uix.widget.Widget.ids ) 或类似的内容(在这里查看: http : //kivy.org/docs/api-kivy.uix.widget.html#kivy.uix.widget.Widget.ids

No it doesn't have to be a button. 不,它不必是按钮。 Without adding any extra objects, you can also use the on_touch_down event of the widget class (which also happens to be the base class of Image). 在不添加任何其他对象的情况下,您还可以使用小部件类的on_touch_down事件(该事件也恰巧是Image的基类)。

<Game>
    Image:
        id: "11"
        center_x: root.width / 2 - root.height * 1/5 * 1.5 
        center_y: root.height * 1/5
        size: root.height / 6, root.height / 6
        source: root.images[0]
        on_touch_down: root.image_press(*args) 

The args contains the extra parameter for the event: http://kivy.org/docs/api-kivy.uix.widget.html?highlight=on_touch_down#kivy.uix.widget.Widget.on_touch_down . args包含事件的额外参数: http : //kivy.org/docs/api-kivy.uix.widget.html?highlight=on_touch_down#kivy.uix.widget.Widget.on_touch_down Here I'm assuming your handler is named image_press and is added to the Game class. 在这里,我假设您的处理程序名为image_press,并添加到Game类中。

Before you start down this road though, consider whether your objective would be fulfilled by the predefined classes eg modifying the background property of a button as you suggested. 但是,在开始这条路之前,请考虑是否可以通过预定义的类来实现您的目标,例如,按照您的建议修改按钮的background属性。 That's likely to be much simpler in the long run if you also want some of the other complex behaviours that the kivy devs have already built in. 从长远来看,如果您还想要奇异的开发人员已经内置的其他一些复杂的行为,那可能会更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM