Simple TK Question - refreshing the canvas when not in focus
Eric Brunel
see.signature at no.spam
Fri May 2 04:53:08 EDT 2008
On Wed, 30 Apr 2008 20:19:32 +0200, blaine <frikker at gmail.com> wrote:
> On Apr 30, 10:41 am, Peter Otten <__pete... at web.de> wrote:
>> blaine wrote:
>> > Still doesn't work. I'm looking into using wx instead...
>>
>> > This is the full code - does it work for anyone else? Just do a echo
>> > 'line 0 0 10 10' > dev.file
>>
>> Haven't tried it, but I think that the problem is that you are updating
>> the
>> UI from the "readthread". A good example to model your app after is
>> here:
>>
>> http://effbot.org/zone/tkinter-threads.htm
>>
>> Peter
>
> Update: Not only is that a good model, its exactly what I'm trying to
> do. Thanks again!
BTW, instead of reading the queue periodically, you can also use the event
loop for that: in the secondary thread, generate a custom event using the
event_generate method. Custom events are enclosed in '<<...>>', the '...'
can be whatever you like. Be sure to generate the event with the
when='tail' option or the event might get handled immediatly. To treat the
event, do a binding on it: it will be called in your main thread. It's a
common trick to switch threads so that GUI events are treated in the main
thread and not in secondary ones.
Example:
------------------------------------------
import threading
import time
import Queue
from Tkinter import *
root = Tk()
q = Queue.Queue()
def thread_action():
i = 1
while 1:
time.sleep(1)
q.put(i)
root.event_generate('<<Ping>>', when='tail')
i += 1
v = StringVar()
def ping_received(event):
v.set(q.get())
Label(root, textvariable=v, width=10).pack()
root.bind('<<Ping>>', ping_received)
th = threading.Thread(target=thread_action)
th.setDaemon(1)
th.start()
root.mainloop()
------------------------------------------
HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
More information about the Python-list
mailing list