Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

time.sleep() and Tkinter after()?

19 views
Skip to first unread message

Davy

unread,
Dec 3, 2008, 10:20:33 PM12/3/08
to
Hi all,

I have used Tkinter after() to do loop update GUI in my previous post.
See http://groups.google.com/group/comp.lang.python/browse_thread/thread/6b616abc236c345b/7df7684d33c969c5#7df7684d33c969c5

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)
print 'get', data_queue.qsize()
draw_canvas(board, canvas_b, x, y, block_width, block_height)
time.sleep(0.3)
##canvas_b.after(300, lambda:draw_canvas_loop(canvas_b))
//--------------------------------

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

And if I use after(), will the code form a recursive function call,
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...).
//------code-----------
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)
canvas_b.after(300, lambda:draw_canvas_loop(canvas_b))
//-------------------------

Best regards,
Davy

Hendrik van Rooyen

unread,
Dec 4, 2008, 2:05:58 PM12/4/08
to pytho...@python.org
"Davy" <zhu..@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

Davy

unread,
Dec 4, 2008, 8:37:04 AM12/4/08
to
On Dec 5, 3:05 am, "Hendrik van Rooyen" <m...@microcorp.co.za> wrote:

> "Davy" <zh...@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

Hendrik van Rooyen

unread,
Dec 4, 2008, 11:27:12 PM12/4/08
to pytho...@python.org

"Davy" <zhli...@gmail.com> wrote:

>Although I don't understand your explanation very well(I guess


>maybe .after() re-schedule is better than .sleep unresponsive in GUI
>application?)I will take it as a golden rule.

I did not in fact try to explain it - I was trying to get you to
think a bit wider, as that way you would get more benefit
than just being given a solution.

As for taking what I say as a golden rule:-

If you hang around this group for long enough, you will learn to
never take what I say as gospel - I often pull the piss - be warned!

8<-----------------------------------

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

You can redraw it to make it look different and position it on
another place, but I have never seen one thing in two places
before. If it is possible I don't know how to do it.

Now if it were a class, you could have multiple instances,
but I have only ever seen one thing, one picture...

- Hendrik

0 new messages