Button Colors in NORMAL and PRESS state in kv language

2,001 views
Skip to first unread message

Martin Fayad

unread,
Jul 10, 2016, 5:56:15 PM7/10/16
to Kivy users support
Hey guys.

I am new to kivy and even thought i read documentation on this problem, I am not able to solve it by myself.

My problem is simple: I have a button and I want to define exactly the colors of normal state and press state - all in kv language.

I have ONLY half solution: I am able to define exacly the color of a button in normal state by this code:

Button:
background_normal: ''
background_color: get_color_from_hex('#918930')


However, when I start to play with command Background_down: '' or something, result doesnt come. 





What Almost DOES work is this code:
Button:
    background_color: get_color_from_hex('#c31f3d')
background_down:'#c31f3d'
But here I get in normal state color as a Default color + Newly defined color, pressed button gets correct color.

If I combine both codes like this:
Button:
    background_normal: '' 
    background_color: get_color_from_hex('#c31f3d')
background_down:'#8FC31F'
I get correct color in normal state, but on press Button doesnt change color so it keeps all the time Normal state color.

So what I need is correct color(defined) in normal state and also in pressed state without any color multiplications.

Thanks for reading and Help
Martin



                                         

werton

unread,
Jul 11, 2016, 1:44:00 AM7/11/16
to Kivy users support
Hi.
background_down:'#c31f3d'
- here you just assigned background_down to non-existent path of the image, because background_down property is not a color, it's Image (take a look at style.kv.).
Standard button does not support the desired you functionality.
But the answer is simple: you should implement your own button class or inherit from existing Button class. It's actually very simple.

понедельник, 11 июля 2016 г., 6:56:15 UTC+9 пользователь Martin Fayad написал:
Message has been deleted

Martin Fayad

unread,
Jul 11, 2016, 5:58:55 AM7/11/16
to Kivy users support
Well in the end I found a solution which is as far as possible from calling elegant, but it works:

Button:
id: BB

background_normal: ''
background_color: get_color_from_hex('#c31f3d')
    background_down:'#8FC31F' # If this is not here, it won't work

on_press: BB.background_color=get_color_from_hex('#8FC31F') # Press state color
on_release: BB.background_color=get_color_from_hex('#c31f3d') # Normal state color

This code is a little bit of engineering and it works i think only by accident

werton

unread,
Jul 11, 2016, 10:26:23 PM7/11/16
to Kivy users support
Also background_down: '#8FC31F' can be simplified to background_down: " , which actually means not to use background_down image.

понедельник, 11 июля 2016 г., 18:58:55 UTC+9 пользователь Martin Fayad написал:
Reply all
Reply to author
Forward
0 new messages