time.sleep() and Tkinter after()?

Hendrik van Rooyen mail at microcorp.co.za
Thu Dec 4 14:05:58 EST 2008


"Davy" <zhu.. at gmail.com> wrote:

> I have used Tkinter after() to do loop update GUI in my previous post.

> And I tried to change after() to time.sleep(), but it seems doesn't
> work at all, the Queue send and receive data properly, but the GUI
> didn't even appear?
> 
> //-----code changed-----
> def draw_canvas_loop(canvas_b):
>     while (True):
>         board = data_queue.get(block = True, timeout=2)

Do you want it to block, or do you want it to time out?

>         print 'get', data_queue.qsize()
>         draw_canvas(board, canvas_b, x, y, block_width, block_height)
>         time.sleep(0.3)

this will make the gui unresponsive for the time

>     ##canvas_b.after(300, lambda:draw_canvas_loop(canvas_b))

and then the control runs off the end of the function.

> So, can I use time.sleep() in GUI application? Or Tkinter scheduler
> just ignore the sleep() function?

time.sleep(sleep_time) will effectively suspend the gui mainloop
(if it is in the mainloop) for the sleep_time, making the gui unresponsive
for that time.  Eschew it here.  Use it in other, non GUI helper threads.

> 
> And if I use after(), will the code form a recursive function call,

only if it is coded that way - yours does not look recursive to me

> and the memory usage will boost as the program goes (I have watched
> the task manager in WinXP and find the python.exe eat more and more
> memory...).

> def draw_canvas_loop(canvas_b):
>     board = data_queue.get(block = True, timeout=1)
>     print 'get', data_queue.qsize()
>     draw_canvas(board, canvas_b, x, y, block_width, block_height)

Here you draw a new canvas object - what has happened to the
previous one? Is it possibly still hanging around? Should you
do something about it?

- Hendrik




More information about the Python-list mailing list