time.sleep() and Tkinter after()?

Discussion in 'Python' started by Davy, Dec 4, 2008.

  1. Davy

    Davy Guest

    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
     
    Davy, Dec 4, 2008
    #1
    1. Advertising

  2. Davy

    Davy Guest

    On Dec 5, 3:05 am, "Hendrik van Rooyen" <> wrote:
    > "Davy" <> 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
     
    Davy, Dec 4, 2008
    #2
    1. Advertising

  3. "Davy" <> 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
     
    Hendrik van Rooyen, Dec 4, 2008
    #3
  4. "Davy" <> 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
     
    Hendrik van Rooyen, Dec 5, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick
    Replies:
    2
    Views:
    2,509
    Roedy Green
    Oct 4, 2003
  2. Gonzalo Moreno
    Replies:
    2
    Views:
    12,521
  3. Sylwia
    Replies:
    0
    Views:
    495
    Sylwia
    Jan 7, 2004
  4. Sam Roberts
    Replies:
    16
    Views:
    402
    Daniel Berger
    Feb 18, 2005
  5. Richard
    Replies:
    7
    Views:
    254
    Richard
    May 22, 2007
Loading...

Share This Page