Search code examples
pythonanimationbuttonkivybackground-color

Prevent animated button from changing color on_press or on_release (No kv / Python portion)


I am trying to make a slider using a button and when pressed reveals two other buttons but does not change colors when pressed solely for aesthetics. I am very comfortable with the .kv language and know you can use
background_color: (1,0,0,1) if self.state == 'normal' else (0,1,0,1)
in the .kv portion of the script and have tried variations of this and using if self.state == 'down': and others such as that in python. However I am trying to learn the dynamic aspect of kivy now and am just starting to play around with animations and would like to be able to do this without using a .kv file or Builder.

The goal is to have two buttons that change background colors beneath a button that simply slides on_press. My issue is that the sliding button is changing colors when I want the background color to remain static and the buttons underneath are not highlighting on_press if I adjust their background colors. I am mainly looking for help with the sliding button remaining one color and am not too concerned if the buttons beneath must remain default. If anyone could offer some help I would greatly appreciate it.

from kivy.animation import Animation
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.behaviors import ButtonBehavior

global active
active = True
class TestApp(App):
    def plop_slide_animation(self, instance):
        global active
        if active == True:
            plop_slide = Animation(pos=(250, 350), duration=0.5)
            active = False
        elif active == False:
            plop_slide = Animation(pos=(250, 200), duration=0.5)
            active = True
        plop_slide.start(instance)
    def ploppi_press(self,instance):
        print('ploppi')
    def plopper_press(self,instance):
        print('plopper')

    def build(self):
        button1 = Button(background_color = (1,.6,1,1), size_hint=(.2, .2), pos=(250, 200), text='PLOP', on_press=self.plop_slide_animation)
        button3 = Button(background_color = (128,0,0,.5), size_hint=(.09, .09), pos=(260, 250), text='ploppi', on_press=self.ploppi_press) #background_color = (128,0,0,.5), 
        button4 = Button(background_color = (0,0,255,.5), size_hint=(.09, .09), pos=(450, 250), text='plopper', on_press=self.plopper_press) #background_color = (0,0,255,.5),
        layout = FloatLayout()

        def change_text(button):
            global active
            if button1.state == 'down':
                print(button1.state)
            if active == True:
                button1.text = 'PLOOOOP'
                print('PLOOOOP')
            if active == False:
                button1.text = 'PLOP'
                print('PLOP')

        button1.bind(on_press=change_text)

        layout.add_widget(button3)
        layout.add_widget(button4)
        layout.add_widget(button1)


        return layout

if __name__ == '__main__':
    TestApp().run()```


Solution

  • Assing empty string to button's images in different states

        button1.background_down = ''
        button1.background_normal = ''
    

    and you get one color all the time.

    But it will be little different color because it will show real color, not dim color.

    Doc: Button


    EDIT: You can also assign one image to another and it will use the same image for both states - and you get the same dim color.

        button1.background_down = button1.background_normal