Threading and tkinter

gert gert.cuykens at gmail.com
Thu Feb 19 05:03:21 EST 2009


On Feb 19, 3:20 am, Steve Holden <st... at holdenweb.com> wrote:
> gert wrote:
> > Can you first explain why x stay's 0 please and how i should update x
> > using threads ?
>
> > fromtkinterimport *
> > 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 withthreading.
>
> 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.
>

Oeps :) Anyway will this x work in tkinter

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,())

while True:
    print(x)



More information about the Python-list mailing list