本文介绍了替换按钮的Kivy可点击图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找一种使用图像的方法,而不是传统的带有纽扣的按钮.
我尝试使用 background_disabled_normal background_disabled_down
这是我的.kv文件中的按钮部分:

Looking for a way to use an image instead of a conventional button with kivy.
I try to use background_disabled_normal and background_disabled_down
This is my button part in my .kv file:

Button:
    on_press: root.do_action()
    background_disabled_normal: str(False)
    Image:
        source: 'icon.png'
        y: self.parent.y + self.parent.height - 250
        x: self.parent.x
        size: 250, 250
        allow_stretch: True

但是不行

推荐答案

background_disabled_normal: str(False)

这应该是图像的文件路径,而不是布尔值的字符串.另外,当按钮的disable属性为True时,这是背景的属性-您确定不想要background_normal吗?

This should be a filepath to an image, not a string of a boolean. Also, this is the property for the background when the button's disabled property is True - are you sure you don't want background_normal?

还有另一种方法可以适合您;该按钮的内容被抽象为ButtonBehavior,可以与任何小部件组合.

There is another way to do this anyway that may suit you; the button stuff is abstracted as ButtonBehavior that may be combined with any widget.

from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image

class ImageButton(ButtonBehavior, Image):
    pass

此ImageButton将具有图像的所有属性(您可以设置源)按钮的所有事件(on_press等).

This ImageButton will have all the properties of an image (you can set the source) and all the events of a button (on_press etc).

这篇关于替换按钮的Kivy可点击图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:27