I am trying to get the below method to incrementally subtract the number which is displayed as the text in the button, but cannot get it to update below 4.
def waterclick(watercount = 5):
water = watercount - 1
waterlabel.config(text = water)
print(water)
full code:
# importing whole module
from tkinter import *
from tkinter import ttk
root = Tk()
#name of the program
label = ttk.Label(root, text = '.0.')
label.grid(row = 0, column = 0)
label.config(text = "Howdy Tkinter")
label.config(foreground = 'white', background = 'black', font = ('impact', 14))
#increments water count by -1
def waterclick(watercount = 5):
water = watercount - 1
waterlabel.config(text = water)
print(water)
#water button
waterlabel = ttk.Button(root, text = 5, command = waterclick)
waterlabel.grid(row = 0, column = 1, columnspan = 2)
mainloop()
CodePudding user response:
The problem is because your function does not change the value of watercount
which is the name of a function argument. The simplest fix is to use a global variable. That approach would be like this:
# importing whole module
from tkinter import *
from tkinter import ttk
root = Tk()
#name of the program
label = ttk.Label(root, text = '.0.')
label.grid(row = 0, column = 0)
label.config(text = "Howdy Tkinter")
label.config(foreground = 'white', background = 'black', font = ('impact', 14))
#increments water count by -1
def waterclick():
global watercount
watercount -= 1 # Update value of global variable.
waterlabel.config(text=watercount)
print(watercount)
#Define global variable.
watercount = 5
#water button
waterlabel = ttk.Button(root, text = 5, command = waterclick)
waterlabel.grid(row = 0, column = 1, columnspan = 2)
mainloop()
A slightly better way to do it would be to use an instance of a tkinter IntVar
class and make it the Label
's textvariable
, that way the label will get updated automatically everytime the IntVar
's value is changed.
# importing whole module
from tkinter import *
from tkinter import ttk
root = Tk()
#name of the program
label = ttk.Label(root, text = '.0.')
label.grid(row = 0, column = 0)
label.config(text = "Howdy Tkinter")
label.config(foreground = 'white', background = 'black', font = ('impact', 14))
#increments water count by -1
def waterclick():
watercount.set(watercount.get()-1) # Update value of tkinter variable.
print(watercount.get())
#Define tkinter Variable.
watercount = IntVar(value=5)
#water button
waterlabel = ttk.Button(root, textvariable = watercount, command = waterclick)
waterlabel.grid(row = 0, column = 1, columnspan = 2)
mainloop()
CodePudding user response:
You are always passing the same number to your waterclick
function, so it always substracts 1 from 5, hence you get 4 as a result.
The way to solve this is to actually read the number that you are displaying in the text each time you call waterclick
and substract it from that number.
To get the current number displayed in your button you can use waterlabel['text']
, as you would do with any other property in Tkinter.
Your code would end up like this:
#increments water count by -1
def waterclick():
water = waterlabel['text'] - 1
waterlabel.config(text = water)
print(water)
#water button
waterlabel = ttk.Button(root, text = 5, command = waterclick)
waterlabel.grid(row = 0, column = 1, columnspan = 2)
CodePudding user response:
Try this:
# importing whole module
from tkinter import *
from tkinter import ttk
root = Tk()
#name of the program
label = ttk.Label(root, text = '.0.')
label.grid(row = 0, column = 0)
label.config(text = "Howdy Tkinter")
label.config(foreground = 'white', background = 'black', font = ('impact', 14))
#increments water count by -1
def waterclick():
water = waterlabel.cget("text") - 1
waterlabel.config(text = water)
print(water)
#water button
waterlabel = ttk.Button(root, text = 5, command = waterclick)
waterlabel.grid(row = 0, column = 1, columnspan = 2)
mainloop()