Search code examples
pythontkintertimeintermediate-code

How do I make a number in a StringVariable increase by 1 every 5 seconds in Tkinter


So i've been coding a Cookie Clicker prototype in Tkinter and I stumbled with this problem:

I first made an option that when pressed, makes every click worth 2 points(It has a price).

Then I wanted to make another option that when pressed, it would add up 1 point every 5 seconds but I haven't found a way to make it work (I have not set the price because it doesn't work yet).

from tkinter import *
import time
#funciones y variables

clicks = 0
incrementer = 1
price1 = 1
loop1 = 0


def sumar():
    global clicks
    clicks += incrementer
    you.set("Has dado " +str(clicks)+ " clicks")

def shop1():
    global clicks
    global incrementer
    if clicks < price1:
        return
    elif clicks >= price1:
        clicks -= price1
        incrementer = 2
        you.set("Has dado " +str(clicks)+ " clicks")
        buy1.destroy()
        buy2.place(x=52, y=155)

def shop2():
    global loop1
    loop1 = int(1)
    buy2.destroy()

while loop1 == 1:
    interface.update()
    time.sleep(5)
    clicks += 1
    you.set("Has dado " + str(clicks) + " clicks")


#Ventana y su configuración

interface = Tk()
interface.title("Cokie Test")
interface.geometry("200x200")
interface.resizable(False, False)
interface.configure(bg="black")

#Botón y changeable value
buy2 = Button(interface, bg="black", fg="white", text="Comprar Auto 1", command=shop2)

buy1 = Button(interface, bg="black", fg="white", text="Comprar x2", command=shop1)
buy1.place(x=62, y=155)
clickerimg = PhotoImage(file="C:/Users/Eduardo/OneDrive/Escritorio/Programming/botoncito.png")
clicker = Button(interface, command=sumar)
clicker.config(image=clickerimg)
clicker.pack(pady=20)

you = StringVar()
you.set("Has dado 0 clicks")
clickss = Label(interface, bg="black",fg="white", textvariable=you)
clickss.place(x=49,y=123)



interface.mainloop()

So I tried this expecting that it would add up 1 point every 5 seconds

def shop2():
    global loop1
    loop1 = int(1)
    buy2.destroy()

while loop1 == 1:
    interface.update()
    time.sleep(5)
    clicks += 1
    you.set("Has dado " + str(clicks) + " clicks")

but it didn't respond


Solution

  • Tkinter widgets have a method named after that can be used to schedule functions to run in the future. If that function itself calls after to reschedule itself, it will continue to run for the life of the application.

    The first argument to after is a delay in milliseconds. The second argument is a reference to a function. Any additional arguments are passed to that function as positional arguments.

    Here's a very simple example:

    import tkinter as tk
    
    def tick():
        value = points.get()
        points.set(value+1)
        root.after(5000, tick)
    
    
    root = tk.Tk()
    
    points = tk.IntVar(root, value=0)
    label = tk.Label(root, textvariable=points)
    label.pack(padx=20, pady=20)
    
    tick()
    
    root.mainloop()