Python Forum
[Tkinter] Make my button text update?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Make my button text update?
#2
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=addToBank
But 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
Reply


Messages In This Thread
Make my button text update? - by Skata100 - Aug-07-2021, 05:16 AM
RE: Make my button text update? - by deanhystad - Aug-07-2021, 05:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] how to make label or button not visible with the place method? nowayj63 2 2,785 Jan-03-2023, 06:29 PM
Last Post: Yoriz
Question [Tkinter] How to make split button? teknixstuff 2 1,059 Jan-03-2023, 06:21 PM
Last Post: Yoriz
  Can't change the colour of Tk button text Pilover 6 14,716 Nov-15-2022, 10:11 PM
Last Post: woooee
  Updating button text based upon different variable values knoxvilles_joker 0 2,231 Apr-18-2021, 04:13 AM
Last Post: knoxvilles_joker
  update text variable on label with keypress knoxvilles_joker 3 4,900 Apr-17-2021, 11:21 PM
Last Post: knoxvilles_joker
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 5,629 Mar-10-2021, 04:21 PM
Last Post: Sir
  tkinter | Button color text on Click Maryan 2 3,359 Oct-09-2020, 08:56 PM
Last Post: Maryan
  How to make button text bold in Tkinter? scratchmyhead 2 12,033 May-16-2020, 02:53 AM
Last Post: scratchmyhead
  [Tkinter] Text Button - How Do I Reduce The Margin? vman44 6 11,215 Apr-27-2020, 10:48 PM
Last Post: Larz60+
  [Tkinter] how to update label text from list Roshan 8 5,448 Apr-25-2020, 08:04 AM
Last Post: Roshan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020