time.sleep() and Tkinter after()?

D

Davy

Hi all,

I have used Tkinter after() to do loop update GUI in my previous post.
See http://groups.google.com/group/comp...abc236c345b/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
 
D

Davy

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?
this will make the gui unresponsive for the time


and then the control runs off the end of the 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.
only if it is coded that way - yours does not look recursive to me


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

Hendrik van Rooyen

Davy said:
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
 
H

Hendrik van Rooyen

Davy said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top