mehdi alizade
mehdi alizade

Reputation: 67

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

Upvotes: 1

Views: 486

Answers (1)

Bahae El Hmimdi
Bahae El Hmimdi

Reputation: 368

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"

Upvotes: 2

Related Questions