As it turns out tkinter.Label
can support int
eger values too, so the way you can do incrementing is by simply using label["text"] += 3
if the starting text
of the label is an int
eger (or float
probably works too).
You also need to not let it create multiple after
"loops" so setting some flags and checking those also should be added, the way I implemented it is by creating an attribute for that same label that tells whether it has started counting and if it hasn't then start.
import tkinter as tk
def start():
if not getattr(label, "counting", False):
update()
label.counting = True
def stop():
root.destroy()
def update():
label["text"] += 3
root.after(500, update)
root = tk.Tk()
root.geometry("420x250")
label = tk.Label(root, background="pink", width=20, height=2, text=0)
label.pack(anchor="w", pady=5)
tk.Button(root, text="Start", width=6, height=2, command=start).pack(
side="left", anchor="nw", padx=2, pady=2
)
tk.Button(root, text="Stop", width=6, height=2, command=stop).pack(
side="left", anchor="nw", pady=2
)
root.mainloop()
Also note that there is no point in assigning the returned values of pack
(or grid
or place
) to a variable, you can just simply create buttons without those variables, label
on the other hand of course needs to be referenced so it's properly made for that.
Also:
I strongly advise against using wildcard (*
) when importing something, You should either import what You need, e.g. from module import Class1, func_1, var_2
and so on or import the whole module: import module
then You can also use an alias: import module as md
or sth like that, the point is that don't import everything unless You actually know what You are doing; name clashes are the issue.