Search code examples
pythonkivykivy-language

I can't get Kivy's button function to work


This is what it looks like when i run it, which is what i want except the flip button does nothingI have tried many fixes for other people but none worked so I am uploading my work in hopes that someone can help me. If you can please comment but you will likely have to put things in the simplest way possible as I don't have a lot of knowledge.

import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.button import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput


class Shark(App):
    def build(self):
        self.max_food = 50
        self.food = self.max_food
        self.respect = 0
        self.layout = BoxLayout(orientation="vertical")
        area1 = BoxLayout(orientation="horizontal")
        area2 = BoxLayout(orientation="horizontal")
        area3 = BoxLayout(orientation="horizontal")
        self.layout.add_widget(area1)
        self.layout.add_widget(area2)
        self.layout.add_widget(area3)
        self.lbl1 = Label(text="{}/{} Food".format(self.food, self.max_food))
        self.lbl2 = Label(text="{} Respect".format(self.respect))
        area1.add_widget(self.lbl1)
        area1.add_widget(self.lbl2)
        btn1 = Button(text="Area 1")
        btn2 = Button(text="Area 2")
        area2.add_widget(btn1)
        area2.add_widget(btn2)
        btn3 = Button(text="Eat")
        btn4 = Button(text="Flip")
        btn4.bind(on_press=self.flip)
        area3.add_widget(btn3)
        area3.add_widget(btn4)
        return self.layout

    def eat(self):
         pass

    def flip(self, obj):
         self.food -= 10
         self.respect += 10
         self.lbl1.text="{}/{} Food".format(self.food, self.max_food)
         self.lbl2.text="{} Respect".format(self.respect)


Shark().run()

Solution

  • Change:

    btn4.bind(on_push=self.flip)
    

    To:

    btn4.bind(on_press=self.flip)
    

    I don't think on_push is a valid action.