Can't pass variables to function with Tkinter in python
Question: I'm trying to take the textvariable from an entry(a) and then put it in a
label in a new window(w).

    
    
    from Tkinter import *
    def abind(avar):
        print avar
        w=Toplevel()
        at=Label(w, text=avar).pack()
        w.mainloop()
    app=Tk()
    at=StringVar()
    a=Entry(app,textvariable=at)
    avar=at.get()
    a.pack()
    a.focus()
    b=Button(app, command=abind(avar)).pack()
    app.mainloop()
    

It either prints blank, if I take the avar out of the parantheses after abind,
or opens a new window immeadiatley and doesn't display the button widget, if I
leave the avar.


Answer: There are two main problems with your code:

  * with `avar=at.get()`, the `avar` variable has the value of the text variable _at that point in time_ , i.e. it is just the empty string
  * with `Button(app, command=abind(avar))`, you are _calling_ the function `abind(avar)` and using its _result_ as a command, i.e. `None`

Also, by doing `b=Button(...).pack()`, `b` is the result of `pack()`, i.e.
`None`. This is not related to your problem, but it's probably not what you
intended, either. Try this:

    
    
    b = Button(app, command=lambda: abind(at.get()))
    b.pack()
    

This uses `lambda` to create a new anonymous function that will get the
current value from `at` using `at.get()` and call `abind` with that value,
setting the text of the `Label` accordingly.

If you want the `Label` to be updated as you type additional text into the
`Entry`, try this:

    
    
    def abind(avar):
        ...
        at = Label(w, textvariable=avar)  # use textvariable
        at.pack                           # pack again
        ...
    ...
    b = Button(app, command=lambda: abind(at)) # pass at itself
    ...
    

