Search code examples
pythonbuttonkivy

How to change background image of button when clicked in kivy?


I have two images for the background a button, One of the images is the default image of the button, I want to change the background image of button, when I click it. This is my code:

class Control_(Screen):
    Status = 0
    print(Status)
    def StatusButton(self):
        if self.Status == 0:
            self.Status = 1
            print(self.Status)
        elif self.Status == 1:
            self.Status = 0   
            print(self.Status) 

and *.kv is:

<Control_>:
    Button:
        background_normal: "on.png"
        size_hint: .3, .2
        pos_hint: {"x":.3, "y":.3} 
        on_release:root.StatusButton()
            

This is my images: enter image description here enter image description here


Solution

  • try that
    
    
    
     <Control_>:
            Button:
                background_normal: "on.png"
                size_hint: .3, .2
                pos_hint: {"x":.3, "y":.3} 
                on_release:self.background_normal="on.png" if background_normal=="off.png" else "off.png"