Dec-28-2021, 05:33 PM
To periodically change the count number you need something that runs periodically. Your approach failed because your up() function is called once instead of once for each number.
tkinter windows have a method .after(msec, func) that will call a function after some time (measured in milliseconds) expires. This can be used to repeatedly call a function to update your counter label.
tkinter windows have a method .after(msec, func) that will call a function after some time (measured in milliseconds) expires. This can be used to repeatedly call a function to update your counter label.
import sys import tkinter as tk increment = 0 end_count = 10 def startCounter(start, end, incr): """I configure the counter""" global increment, end_count counter.set(start) increment = incr end_count = end def update(): """I update the count label""" if increment > 0: count = min(end_count, counter.get() + increment) else: count = max(end_count, counter.get() + increment) counter.set(count) root.after(1000, update) # Run myself again 1 second from now. root = tk.Tk() counter = tk.IntVar(root, 0) tk.Label(root, textvariable=counter, width=10).pack() tk.Button(root, text='Up', width=10, command=lambda: startCounter(0, 10, 1)).pack() tk.Button(root, text='Down', width=10, command=lambda: startCounter(10, 0, -1)).pack() tk.Button(root, text='Exit', width=10, command=sys.exit).pack() update() # Begin the update "loop" root.mainloop()