Tk not displaying correctly

Discussion in 'Python' started by Tuvas, Dec 21, 2005.

  1. Tuvas

    Tuvas Guest

    I am building a TK interface, that has several threads. If all threads
    are running actively, including the main one, the interface doesn't
    update. For example, I have something like this, although more complex

    import time, threading

    master=Tk()

    def thread:
    global x
    x=0
    while(TRUE):
    x=x+1
    Label(master,text=x).grid(row=0,column=0)
    time.sleep(1)

    but=Button(master,command=command,text="hit me")
    but.grid(row=0,column=0)

    def command:
    while(x<100):
    time.sleep(1)
    x=0

    t=threading.Thread(target=thread)
    t.setDaemon(1)
    t.start()

    master.mainloop()

    What (I think) will happen is when you hit the button, until x=100, the
    display will stop updating, and when the command has left it's thread,
    it will return to updating again. Is there a way to make it so it
    always will update, irreguardless if it's in a seporate thread, perhaps
    calling a new thread that calls mainloop? Thanks!
     
    Tuvas, Dec 21, 2005
    #1
    1. Advertising

  2. Tuvas

    Paul Rubin Guest

    "Tuvas" <> writes:
    > What (I think) will happen is when you hit the button, until x=100, the
    > display will stop updating, and when the command has left it's thread,
    > it will return to updating again. Is there a way to make it so it
    > always will update, irreguardless if it's in a seporate thread, perhaps
    > calling a new thread that calls mainloop? Thanks!


    Yes, you have to call mainloop in the separate thread. Be careful
    because tkinter is not thread safe. You have to communicate with the
    gui thread through some synchronized mechanism like queues. I usually
    use the w.after operation to make a polling loop that checks a queue
    for gui-updating events. The events themselves are just callables
    that do gui operations when called.
     
    Paul Rubin, Dec 21, 2005
    #2
    1. Advertising

  3. Tuvas

    Tuvas Guest

    Any way you could provide a fairly simple example, or a website that
    shows it? I understand that I must create a thread for mainloop,
    however, I can't see how to make that work, every time I do, it ends
    the program only a few seconds late. Will I have to make an even for
    all buttons, etc that are on the GUI? Wow, sounds like lots of work...
    Anyways, thanks!
     
    Tuvas, Dec 22, 2005
    #3
    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. Raterus
    Replies:
    1
    Views:
    589
    Guest
    Jun 5, 2004
  2. Paul Jacobs
    Replies:
    3
    Views:
    4,585
    Neomorph
    Aug 25, 2003
  3. tshad
    Replies:
    0
    Views:
    423
    tshad
    Feb 16, 2007
  4. NJP
    Replies:
    1
    Views:
    690
    Kevin
    Mar 5, 2007
  5. Norman
    Replies:
    8
    Views:
    552
    Neredbojias
    Oct 13, 2007
Loading...

Share This Page