'How do I start a timer when I click on an entry widget

I'm trying to start a timer when I click on an entry widget, but I have no clue how to do it.

I've got a program that loads up a tk window, and a timer starts as soon as the window opens. I have a button that resets the timer, but I would like to have it so that the timer starts only when I click inside the entry box. I've also got a bind setup so that when I hit enter the text is passed into a function and printed.

from tkinter import *
from tkinter import messagebox
counter = 0


def counterlabel(label):
    def count():
        global counter

        counter += 1
        label.config(text=str(counter))
        label.after(1000, count)

    count()


def game(*args):
    x = entry.get()
    print(x)


def reset(label):
    global counter
    counter = 0
    label.config(text=str(counter))


root = Tk()
root.title("hello")
quit = Button(root, command=lambda: root.destroy())
quit.pack()

label = Label(root)
label.pack()

button = Button(
    root, text="hit me to reset timer",
    command=lambda: reset(label))
button.pack()

entry = Entry(root)
entry.bind("<Return>", game)
entry.pack()

counterlabel(label)
root.mainloop()


Solution 1:[1]

Why not bind Entry widget with <Button-1> sequence with counter, So when a user clicks on the Entry widget the timer starts and the rest is as you've programmed.

Try this:

entry.bind( "<Button-1>",lambda e: counterlabel(label) )

Also I don't know why you made this function so complicated, when you could've just made one function instead of one inside one, in case if you have other plans to add more code in the functions later.

Could be just this

def count(evt=None):
    global counter
    counter += 1
    label.config(text=str(counter))
    label.after(1000, count)

...

root=Tk()

...

entry.bind( "<Button-1>", count) )

To stop the timer when you press the button

For that you have to get an id of the after() function and pass it to after_cancel(id) function ( id = after(1000, count) ). It will start again when you click on the Entry.

Here is the complete code

from tkinter import *
from tkinter import messagebox

counter = 0
timer_id = None

def count(evt=None):
    global counter, timer_id
    entry.unbind('<Button-1>')  # So the timer won't go crazy on multiple presses.
    counter += 1
    label.config(text=str(counter))
    timer_id = label.after(1000,count)

def game(*args):
    x=entry.get()
    print(x)

def reset(label):
    global counter
    counter=0
    if timer_id: label.after_cancel(timer_id)
    entry.bind('<Button-1>', count)    # When reset the user can start again 
    label.config(text=str(counter))

root=Tk()
root.title("hello")
quit=Button(root,command=lambda: root.destroy())
quit.pack()

label=Label(root, text='0')
label.pack()

button=Button(root,text="hit me to reset timer",command=lambda: reset(label))
button.pack()

entry=Entry(root)
entry.bind("<Return>", game)
entry.bind('<Button-1>', count)
entry.pack()

root.mainloop()

Hope this helped

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1