Python Forum
Show the result of every count
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Show the result of every count
#7
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.
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()
George87 likes this post
Reply


Messages In This Thread
Show the result of every count - by George87 - Dec-28-2021, 11:42 AM
RE: Show the result of every count - by menator01 - Dec-28-2021, 01:48 PM
RE: Show the result of every count - by George87 - Dec-28-2021, 02:17 PM
RE: Show the result of every count - by menator01 - Dec-28-2021, 03:36 PM
RE: Show the result of every count - by menator01 - Dec-28-2021, 04:00 PM
RE: Show the result of every count - by George87 - Dec-28-2021, 05:16 PM
RE: Show the result of every count - by deanhystad - Dec-28-2021, 05:33 PM
RE: Show the result of every count - by menator01 - Dec-28-2021, 06:04 PM
RE: Show the result of every count - by deanhystad - Dec-28-2021, 08:58 PM
RE: Show the result of every count - by menator01 - Dec-28-2021, 09:04 PM
RE: Show the result of every count - by deanhystad - Dec-28-2021, 10:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] How to get the result of a ping to show in tkinter? jacklee26 6 8,054 Feb-10-2023, 01:12 PM
Last Post: NebularNerd

Forum Jump:

User Panel Messages

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