I have a python 3 program and need a delay of 20 Seconds I have tried time.sleep(20) but that causes loads of problems like program stop responding on windows by the way I'm using tkinter, does any one no a alternative way of making a delay or sample code is there a way I can do this with loop?
Asked
Active
Viewed 185 times
1
-
*like program stop responding on windows*: that is not a problem but the *natural* behavior – Billal Begueradj Jun 19 '16 at 11:29
-
1alternative way to make delay? – JJLongOne Jun 19 '16 at 11:31
-
http://stackoverflow.com/questions/15167334/alternative-to-pythons-time-sleep – Billal Begueradj Jun 19 '16 at 11:39
-
1very good! thank you! http://stackoverflow.com/questions/15167334/alternative-to-pythons-time-sleep – JJLongOne Jun 19 '16 at 11:44
1 Answers
1
Tkinter provides a way to schedule a function to run in the future with the universal method after
. For example, to call do_something
in 20 seconds you would do it like this:
root = tk.Tk()
...
root.after(20000, do_something)

Bryan Oakley
- 370,779
- 53
- 539
- 685
-
That is more effective and appropriate when working with Tkinter than what is mentioned in the link I gave him. – Billal Begueradj Jun 19 '16 at 12:29
-
That good if your working with tkinter elements, but in my case I was working with functions – JJLongOne Jun 19 '16 at 13:28
-
-
@JJLongOne, don't sleep, wait or do CPU-intensive processing in the main thread. It needs to be responsive to handle its event loop and interface to the platform's window manager. Functions such as `after` allow cooperating with the toolkit's event loop. It's not just for working with Tk elements. – Eryk Sun Jun 19 '16 at 14:28
-
1@JJLongOne To be super clear, `do_something` in the .after call can be *any* function. tk does not know or care what `do_something` does. It just calls it. The one constraint is that `do_something` should execute quickly, so that tk can attend to other events. If necessary, `do_something` should do just part of its work and then schedule itself to be called again. Searching SO for `[tkinter] root.after` will lead you to many examples. – Terry Jan Reedy Jun 19 '16 at 16:18