Threading and tkinter

Steve Holden steve at holdenweb.com
Thu Feb 19 03:20:13 CET 2009


gert wrote:
> Can you first explain why x stay's 0 please and how i should update x
> using threads ?
> 
> from tkinter import *
> from _thread import start_new_thread
> from time import sleep
> 
> x=0
> def weegbrug(x):
>     while True:
>         x=x+1
>         sleep(0.5)
> start_new_thread(weegbrug,(x,))
> 
> root = Tk()
> v = StringVar()
> v.set("00000")
> txt = Label(root, textvariable=v, width=800, height=600, bg="yellow",
> font=("Helvetica", 300))
> txt.pack(expand=YES, fill=BOTH)
> root.title("Weegbrug")
> root.after(500, lambda:v.set(x))
> root.mainloop()
> 
The reason x stays at zero has nothing to do with threading.

The statement

    x=x+1

(which, by the way, should stylistically be written

    x = x + 1

if you want your code to be readable) doesn't change anything outside
the function. Function arguments are values: since x is a parameter of
the function, it exists only inside the function call's namespace.

regards
 Steve

-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list