简体   繁体   中英

Pass Multiple Arguments to Callback for a StringVar in Tkinter

I am working with a small grid of Entry widgets. It could be 5 x 1 or 5 x 5 or other sizes in between or near those sizes. When data is entered into an Entry widget, I'd like to be able to pass this on, eventually, to a 2D array of str() variables.

The problem is if I have a number of StringVar() variables, when I use StringVar.trace(), is there a way to pass extra arguments to the callback function?

I currently have:

import Tkinter
guiRoot = Tkinter.Tk()
hWindow = Tkinter.Frame(guiRoot)
hWindow.grid();

def callback(*args):
    print "Current value:",sv.get()

sv = Tkinter.StringVar()
sv.trace("w", callback)
Tkinter.Entry(hWindow, textvariable=sv).grid(row=1,column=1)
guiRoot.mainloop()

But I'd like to pass on to the callback an X and Y coordinate as well. Is that possible?

And if I can do it with a callback for a StringVar, can I do it for an Entry widget?

And if I do it with an Entry widget, is there a way I can pass the event to the callback (along with the X and Y info, too) through to the callback function?

In other words, is there some way I can have:

def callback(*args):
    print "Array coordinates of Entry (or Stringvar):",x,y
    print "Current value:",sv.get

(Yes, I know I'd have to have some way to reference x and y in that callback - so I'd need to know how to do that, too.)

Yes, you can add whatever arguments you want.

def callback(x,y,*args):
    print "x:", x, "y:", y
    print "args:", args
...
sv.trace("w", lambda name1, name2, op, x=100, y=200: callback(x,y,name1,name2,op))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM