Threading and tkinter

Hendrik van Rooyen mail at microcorp.co.za
Wed Feb 18 02:25:24 EST 2009


"gert" <gert.cuykens at gmail.com>wrote:


> After reading the docs and seeing a few examples i think this should
> work ?
> Am I forgetting something here or am I doing something stupid ?
> Anyway I see my yellow screen, that has to count for something :)
> 
> from tkinter import *
> from threading import Thread
> 
> class Weegbrug(Thread):
>     def __init__(self,v):
>         self.v=v
>         Thread.__init__(self)
>     def run(self):
>         while True:
>             with open('com1','r') as f:
>                  for line in f:
>                      self.v.set(line[2:-1])

It is in general not a good idea to directly
access GUI variables from outside the
GUI main loop.
There is a recipe for doing this sort of thing,
but as usual I have lost the reference.
What it does is that instead of interfering directly
as above, you put the data on a queue.

Then, you use the after() call to set up a call
to a routine that reads the queue, and configures the
display, and then uses after again to call itself again
after a time, thereby keeping the GUI stuff in the GUI 
mainloop.

HTH - Hendrik






More information about the Python-list mailing list