Search code examples
pythonif-statementkivy

If statement not being executed in Kivy


I'm creating an application in Kivy that allows users to keep track of statistics while watching a wheelchair basketball game.

I've created buttons representing the statistics and when pressed I expect them to update the Player's stats (dictionary within an instance of a Player class). If the button is the 'foul' button, I expect it to update the '.fouls' stat of the Team class instance as well as the players stats. When executing it seems to ignore the 'if' statement and go straight to the 'else' statement. in the 'update_stats' function (at the bottom). Does anyone know why this might be happening?

I've asked ChatGPT and it can't seem to figure out what the problem is. I've used print statements and it's clear that only the else statement is being executed.

I've included what I think is the minimum version of the code below but let me know if you need more info. Apologies, probably a super basic question but I am very new to python and kivy!

class Team:
    def __init__(self, team_name):
        self.team_name = team_name
        self._fouls = 0
        self.players = []
class Player:
    def __init__(self, name):
        self.name = name
        self.stats = {
            "Fouls": 0,
            "2-pt FG MADE": 0,
            "2-pt FG Missed": 0,
            "Rebounds": 0,
            "Assists": 0,
        }
class HeaderRowWidget(GridLayout):
    def __init__(self, **kwargs):
        super(HeaderRowWidget, self).__init__(**kwargs)
        self.cols = 6

        self.add_widget(Label(text="Player Name"))
        self.add_widget(Label(text="Shirt Number"))
        self.add_widget(Label(text="Fouls"))
        self.add_widget(Label(text="2-pt FG MADE"))
        self.add_widget(Label(text="2-pt FG Missed"))
        self.add_widget(Label(text="Rebounds"))

class MyRowWidget(GridLayout):
    def __init__(self, player, team_instance, **kwargs):
        super(MyRowWidget, self).__init__(**kwargs)
        self.cols = 6
        self.player = player
        self.name_label = Label(text=player.name)
        self.shirt_number_label = Label(text="default")
        self.add_widget(self.name_label)
        self.add_widget(self.shirt_number_label)
        self.team_instance = team_instance

        # Add buttons to the layout
        button_labels = ["Fouls", "2-pt FG MADE", "2-pt FG Missed", "Rebounds"]
        self.buttons = {}

        for label in button_labels:
            button = Button(text="+")
            self.buttons[label] = button
            button.bind(on_press=self.update_stats)
            self.add_widget(button)

    def update_stats(self, instance):
        button_text = instance.text.strip()  # Get the text of the button that was pressed

        if button_text == "Fouls":  # Check against the button's text
            self.player.stats["Fouls"] += 1
            print("players fouls increased")
            self.team_instance.fouls += 1
            print("teams fouls increased")

        else:
            for label, button in self.buttons.items():
                if button == instance:
                    self.player.stats[label] += 1
                    print("stat updated")

Solution

  • All your buttons have text "+". Compare like this instead:

    def update_stats(self, instance):
        if instance == self.buttons["Fouls"]:  # Check against the button dict
            [...]