Tk not displaying correctly

T

Tuvas

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!
 
P

Paul Rubin

Tuvas said:
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.
 
T

Tuvas

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!
 

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,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top