time.sleep() and Tkinter after()?

Davy zhushenli at gmail.com
Thu Dec 4 14:37:04 CET 2008


On Dec 5, 3:05 am, "Hendrik van Rooyen" <m... at microcorp.co.za> wrote:
> "Davy" <zh... 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?

Hi Hendrik, nice comments :-)
Do you think block and time out are contradict to each other?

>
> >         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.

Although I don't understand your explaination very well(I guess
maybe .after() re-schedule is better than .sleep unresponsive in GUI
application?)I will take it as a golden rule.

>
>
>
> > 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?

Yeah, I forgot to *delete* the rectangle object I generate before.
One more question, besides create/delete method, can I just create two
rectangles (one black/one white), and assign two tags to these two
rectangles, and place rectangle by just using tags(that is, can I
place one object on several places of the canvas)?

>
> - Hendrik




More information about the Python-list mailing list