Aug-07-2021, 05:37 AM
(This post was last modified: Aug-07-2021, 05:37 AM by deanhystad.)
The easiest way to do this is make bankValue a StringVar or IntVar. In this example I make it a StringVar so I can format it to have two decimal places.
import tkinter as tk def addToBank(amount): balance = float(bankValue.get()) + amount bankValue .set(f'{balance:.2f}') gameWindow = tk.Tk() bankValue = tk.StringVar() bankValue.set('0.00') button1 = tk.Button(gameWindow, command=lambda:addToBank(1.25), textvariable=bankValue) button1.pack() gameWindow.mainloop()This code sets your callback function to None.
command=addToBank(1)command is None because addToBank(1) calls the function and uses the return value (None) and the value for command. You probably wanted this:
command=addToBankBut this does not provide an argument for "amount" in addToBank(). You could get around this by providing a default value for amount.
def addToBank(amount=1.25): balance = float(bankValue.get()) + amount bankValue .set(f'{balance:.2f}')You can also use a lambda expression to bind an argument to the function. The same kind of thing can be done using functools.partial