Threading and tkinter

gert gert.cuykens at gmail.com
Thu Feb 19 13:35:40 EST 2009


Hope you do not mind ignoring part of answers, so I can figure out
more why things work the way they are.
This two examples work, what i do not understand is that in function
display i do not have to declare root, v or x ?

----------
example 1
----------
from tkinter import *
from _thread import start_new_thread
from time import sleep

x=0
def weegbrug():
    global x
    while True:
        x=x+1
        sleep(0.5)
start_new_thread(weegbrug,())

def display():
    v.set(x)
    root.after(500, lambda:display())

root = Tk()
v = StringVar()
txt = Label(root, textvariable=v, width=800, height=600, bg='yellow',
font=('Helvetica', 300))
txt.pack(expand=YES, fill=BOTH)
root.title('Weegbrug')
root.overrideredirect(1)
root.geometry('%dx%d+0+0' % (root.winfo_screenwidth(),
root.winfo_screenheight()))
root.after(500, lambda:display())
root.mainloop()


----------
example 2
----------
from tkinter import *
from threading import Thread
from time import sleep

class Weegbrug(Thread):
    def __init__(self):
        self.x=0
        Thread.__init__(self)
    def run(self):
        while True:
            self.x=self.x+1
            sleep(0.5)
w = Weegbrug()
w.start()

def display():
    v.set(w.x)
    root.after(500, lambda:display())

root = Tk()
v = StringVar()
txt = Label(root, textvariable=v, width=800, height=600, bg='yellow',
font=('Helvetica', 300))
txt.pack(expand=YES, fill=BOTH)
root.title('Weegbrug')
root.overrideredirect(1)
root.geometry('%dx%d+0+0' % (root.winfo_screenwidth(),
root.winfo_screenheight()))
root.after(500, lambda:display())
root.mainloop()



More information about the Python-list mailing list