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?
So i have an integer 'bankValue' that is increased by a function 'addToBank'.

The buttons text displays 'bankValue', and should update upon click, but doesn't.

from tkinter import *
import tkinter.messagebox as mb
import os

# the in-game bank value, your total 'currency'
bankValue = 0

# function that decides how much to add to bankValue
def addToBank(amount):
    global bankValue
    bankValue += amount

# "gameWindow" = tk interface
gameWindow = Tk()

# heightxwidth of interface

# title on top bar
gameWindow.title("WTFIT v0.05")

# button click = bankValue + amount (amount can be changed)
button1 = Button(gameWindow, command=addToBank(1), text=bankValue, width = 10, height = 2, bg = 'cyan')

# placement of button, y=25)

# shows whats happening
I want the button to increase the value of 'bankValue', and display it as well.
I don't need it to display on the button either, if I can have that number display anywhere that would be awesome.

Thank you!
- Skata100
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()
button1 = tk.Button(gameWindow, command=lambda:addToBank(1.25), textvariable=bankValue)

This code sets your callback function to None.
command is None because addToBank(1) calls the function and uses the return value (None) and the value for command. You probably wanted this:
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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Updating button text based upon different variable values knoxvilles_joker 0 770 Apr-18-2021, 04:13 AM
Last Post: knoxvilles_joker
  update text variable on label with keypress knoxvilles_joker 3 1,406 Apr-17-2021, 11:21 PM
Last Post: knoxvilles_joker
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 2,188 Mar-10-2021, 04:21 PM
Last Post: Sir
  tkinter | Button color text on Click Maryan 2 1,405 Oct-09-2020, 08:56 PM
Last Post: Maryan
  How to make button text bold in Tkinter? scratchmyhead 2 3,476 May-16-2020, 02:53 AM
Last Post: scratchmyhead
  [Tkinter] Text Button - How Do I Reduce The Margin? vman44 6 4,478 Apr-27-2020, 10:48 PM
Last Post: Larz60+
  [Tkinter] how to update label text from list Roshan 8 2,369 Apr-25-2020, 08:04 AM
Last Post: Roshan
  [Tkinter] how to celect com port from spinbox and make connect button 00alkskodi00 0 993 Apr-20-2020, 02:26 PM
Last Post: 00alkskodi00
  [PyQt] Pyqt5: How do you make a button that adds new row with data to a Qtablewidget YoshikageKira 6 3,354 Jan-02-2020, 04:32 PM
Last Post: Denni
  Make Label Text background (default color) transparent using tkinter in python barry76 1 15,466 Nov-28-2019, 10:19 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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