-2

I can't seem to update my money counter(var money) when its labeled, I have a button that's supposed to add 0.1 to moneyNum but the money counter doesn't change. I'm new to stack overflow and would appreciate any help.(btw sry if its REALLY messy)

from tkinter import *
import random
from time import sleep
root = Tk()
root.geometry('320x320')

#spacing
spacingTitle = Label(root, text=" \n\n\n\n\n\n\n\n\n")



#title
title = Label(root, text="                        \bGamblers Dream\b", font="Helvetica", fg="red")
titleWelcom = Label(root, text="                        Welcom to...")
titleWelcom.grid()
title.grid()


#money counter
moneyNum = float(10.0)
money = Label(root, text="money:" + str(moneyNum), font="Helvetica")
money.grid(row=3, column=0)

#moneyClicker
def moneyButtonAdder():
    global moneyNum
    moneyNum = moneyNum + 0.1
moneyClicker = Button(root, text="click", fg="green", command=moneyButtonAdder)
moneyClicker.grid(row=14)


root.mainloop()
  • Does this answer your question? [making-python-tkinter-label-widget-update](https://stackoverflow.com/questions/1918005) – stovfl May 11 '20 at 16:08
  • Just update `moneyNum` will not update `money` label. Add `money.config(text="money:"+str(moneyNum))` after updating `moneyNum`. – acw1668 May 11 '20 at 16:10

1 Answers1

0

The problem is that once you create a label, you pass the string to it. Label displays the string, and:

  1. changing a string object does not change the label text
  2. changing the integer does not change the string - it lost the whole connection when the new string object was created So everything is not as procedural as you would have hoped it is. The solution - use StringVar objects and detect value changes - see this. So, the solution is:
from tkinter import *

class Observed(object):
    """adapted copy from https://stackoverflow.com/a/6192298/10713244"""
    def __init__(self):
        self._observed = 10.0
        self._observers = []

    @property
    def observed(self):
        return self._observed

    @observed.setter
    def observed(self, value):
        self._observed = value
        for callback in self._observers:
            print('announcing change')
            callback(self._observed)

    def bind_to(self, callback):
        print('bound')
        self._observers.append(callback)


class Observer(object):
    def __init__(self, data):
        self.text = ''
        self.data = data
        self.data.bind_to(self.update)

        self.tkinter_init()
    def update(self, observed):
        self.text = 'float: '+str(data._observed)

        self.tkinter_update()

    def tkinter_init(self):
        self.tk = Tk()
        self.tvar = StringVar()
        self.label = Label(textvariable=self.tvar)
        self.label.pack()
    def tkinter_update(self):
        self.tvar.set(self.text)


if __name__ == '__main__':
    data = Observed()
    label = Observer(data)
    print(label.text)
    data.observed = 10.0
    print(label.text)

    def dec(): data.observed -= 0.1
    Button(label.tk, text='decrease', command=dec).pack()

    label.tk.mainloop()

Hope that's helpful!

rizerphe
  • 1,340
  • 1
  • 14
  • 24