Tk not displaying correctly

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tuvas

    #1

    Tk not displaying correctly

    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,te xt=x).grid(row= 0,column=0)
    time.sleep(1)

    but=Button(mast er,command=comm and,text="hit me")
    but.grid(row=0, column=0)

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

    t=threading.Thr ead(target=thre ad)
    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!

  • Paul Rubin

    #2
    Re: Tk not displaying correctly

    "Tuvas" <tuvas21@gmail. com> writes:[color=blue]
    > 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![/color]

    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.

    Comment

    • Tuvas

      #3
      Re: Tk not displaying correctly

      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!

      Comment

      Working...