1

I am creating a program for scorekeeping basketball. I have a few different files with classes and what not. My main problem is trying to update the points of each players.

For example:

I have a button set up on the screen;

pointsButton = Button(root, text='1PT', command=addPoint)
pointsButton.grid(row=0, column=1)

And a label next to that, that calls the points of a specific player (supposedly).

plabel = Label(root, text=(str(p.points)), relief='groove', bg='#41B6E6', fg = '#DB3EB1', padx=numX, pady=numY)
plabel.grid(row=rowNumber, column=4)

Here's the code from my player class that is probably needed to understand my problem.

class BasketballPlayer:
    #Constructor
    def __init__(self , preName, lastName, jerseyNumber):
        self.preName = preName
        self.lastName = lastName
        self.jerseyNumber = jerseyNumber
        self.points = 0
        self.assists = 0
        self.rebounds = 0
        self.steals = 0
        self.blocks = 0
        self.fouls = 0
        self.threePointers = 0
        self.careerHighPoints = 0
        self.careerHighAssists = 0
        self.careerHighRebounds = 0
        self.careerHighSteals = 0
        self.careerHighBlocks = 0
        self.careerHighThreePointers = 0

And a couple functions from the class:

    def addPoints(self, p):
        self.points += p

    def incrementOnePoint(self):
        self.points += 1

    def getPoints(self):
        return self.points

Here's a couple functions I've tried.

def addPoint():
        p.incrementOnePoint()
        plabel.config(text=p.points)

Or:

def addPoint():
        p.addPoints(1)
        plabel.config(text=p.points)

I really thought it would just automatically update because I'm adding a integer to a variable, but it's not updating at all.

Here is a minimal reproducible example as requested in the comments.

from tkinter import *

root = Tk()

class bballPlayer:
    def __init__(self):
        self.points = 0
    
    def incrementOnePoint(self):
        self.points += 1
    
    def getPoints(self):
        return self.points

    
def addOnePoint():
    p.incrementOnePoint
    global pointslabel
    pointslabel.config(text=str(p.points))

p = bballPlayer()
    
pointslabel = Label(root, text=str(p.points))
pointslabel.grid(row=0, column=1)

btn = Button(root, text='Add Point', command=addOnePoint)
btn.grid(row=0, column=0)

root.mainloop()
  • 4
    Does this answer your question? [Update Tkinter Label from variable](https://stackoverflow.com/questions/2603169/update-tkinter-label-from-variable) – frozen Aug 08 '20 at 22:18
  • Offhand, I would agrree that the code currently in your question looks like it would work. If you [edit] your question and replace it wiht a [mre] the reproduces the problem, then someone should be able to help solve the problem. – martineau Aug 08 '20 at 22:39
  • Don't know if there's a way I can reply directly to you, @martineau but I edited the post so that it has a minimal reproducible example. Hope this works! Thanks for the advice. – vintagesteam Aug 08 '20 at 23:19
  • @frozen I'm sure it would but I don't completely understand it yet. I'm pretty new to programming and got some help on this project. Thanks for the advice. – vintagesteam Aug 08 '20 at 23:20
  • vintagesteam: Putting @ followed be a username in a comment it how to reply to someone (you did it right). – martineau Aug 08 '20 at 23:35
  • Your first try should work. – acw1668 Aug 09 '20 at 01:19

1 Answers1

1

Ahhh, now I see the problem, your code isn't calling the incrementOnePoint() method (only referencing its name).

def addOnePoint():
    p.incrementOnePoint()  # ADD MISSING PARENTHESES TO END.
    global pointslabel  # NOT STRICTLY NECESSARY BTW.
    pointslabel.config(text=str(p.points))
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Okay that fixed my sample code. Unfortunately my actual code has a for loop that goes through a list and makes labels for all of the players. I think that may be where my problem is. – vintagesteam Aug 09 '20 at 01:51
  • Then you can pass the player instance and the associated label to `addOnePoint()` function. – acw1668 Aug 09 '20 at 03:26
  • vintagesteam: Can't answer the unasked question. You're probably creating the labels incorrectly (or the callback part of that). – martineau Aug 09 '20 at 06:05