更改Kivy中的点击按钮颜色

时间:2016-04-08 20:08:13

标签: python button kivy

我正在使用Kivy和Python 2.7。我熟悉如何改变静态按钮本身的颜色,但是当你按下它时如何改变按钮的颜色?默认为蓝色。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

根据reference for Button,属性background_down存储了在按下Button时用于背景的图像的路径。这是默认值:

background_down = StringProperty(
    'atlas://data/images/defaulttheme/button_pressed')

您可以将该属性更改为指向其他image oratlas

答案 1 :(得分:1)

Kivy框架使用button_normal和button_down的背景图片,background_color只有色调,所以kv语言中的这个可能不会像你期望的那样:

<Button>:    
    background_color: 1, 0, 0  # Tints the button red
    background_normal: 'images/button_normal.png'  # A clear image gives a bright red. 
    background_down: 'images/button_down.png'  # A gray image gives a duller red.
    border: (2, 2, 2, 2)  # Don't stretch the outer two pixels on each edge when resizing.

这种风格让你可以说一个沉闷的边框和明亮的内部,并按下按钮按下它们。如果你使用这个系统,图像将导入颜色被忽略。要解决此问题并解决您的问题,请删除background_color:

<Button>:    
    background_normal: 'images/button_normal.png'  # Eg. A red button
    background_down: 'images/button_down.png'  # Eg. A green button
    border: (2, 2, 2, 2)  # Don't stretch the outer two pixels on each edge when resizing.

将按钮的颜色更改为您在图像中所做的任何内容。值得注意的是,Kivy非常擅长拉伸图像,所以如果你有单色按钮或小边框,你只需要一个小图像,我使用8x8像素。

相关问题