praveen kumar
praveen kumar

Reputation: 75

after cancel method tkinter

I have been through many examples about after cancel method in Tkinterbut I am not clear about that.In my code I am using the after method to enter the value of s in entry box after a certain time delay.Now i need to stop that after the cycle is completed when a button is pressed.

def read_pressure():
    global s
    s+=1
    E3.delete(0,'end')
    E3.insert(0,s)
    top.after(1000, lambda:read_pressure())

Now i need to stop these loop using a button.How to do that?? I am using python 3.5 With the help of Dan i can able to stop after method.But it freezes my gui and I can't able to recall the after method. How to do that?!

Upvotes: 1

Views: 2344

Answers (1)

Dan D.
Dan D.

Reputation: 74655

You need to keep the return value around:

handle = top.after(1000, lambda:read_pressure())

And then when the button is clicked do:

if handle:
   top.after_cancel(handle)
   handle = None

Both with global handle. I would prefer to make these methods and use self to store state than mutate globals.

Upvotes: 1

Related Questions