pwpropipe
pwpropipe

Reputation: 33

How can I access the particular tkinter StringVar that triggers a trace callback?

I'm trying to use a trace callback to validate inputs to a set of combo boxes but the arguments I get in the callback are just a string representation of the internal name. Is there a way to get a reference to the actual variable triggering the trace callback or a way to get at the variable using the internal name like PY_VAR1?

from Tkinter import *
import ttk


def validate(self, *args):
    print(self)
    print(self.get())

master = Tk()

cb1_var = StringVar()
cb2_var = StringVar()
cb1_var.trace('w', validate)
cb2_var.trace('w', validate)

cb1 = ttk.Combobox(master, textvariable=cb1_var)
cb2 = ttk.Combobox(master, textvariable=cb2_var)

cb1.grid(row=0, column=0, sticky='NW')
cb2.grid(row=1, column=0, sticky='NW')

mainloop()

Fails on trying to call self.get() since self is just that string representation, not the actual StringVar. I don't want to have a specific callback for each StringVar since the actual interface has around 30 boxes I all want validated by the same criteria.

Upvotes: 3

Views: 3726

Answers (2)

Francesco
Francesco

Reputation: 513

I know this is an old post, gut I found a way to use the internal variable name. Modify your validate function like this:

def validate(self, *args):
    var_name = args[0]
    var = IntVar(name=var_name)
    print(var.get())

master = Tk()

cb1_var = StringVar()
cb2_var = StringVar()
cb1_var.trace('w', validate)
cb2_var.trace('w', validate)

Upvotes: 0

Nae
Nae

Reputation: 15345

You could simply pass the arguments you want by making use of lambda statement for anonymous functions. Replace:

def validate(self, *args):
    print(self)
    print(self.get())

...

cb1_var.trace('w', validate)
cb2_var.trace('w', validate)

with:

def validate(var):
    print(var)
    print(var.get())

...

cb1_var.trace('w', lambda *_, var=cb1_var: validate(var))
cb2_var.trace('w', lambda *_, var=cb2_var: validate(var))

If you use multiple objects that are related, simply use collection types. For the example in the question, I see a list should be a good fit.

See the example below:

try:                        # In order to be able to import tkinter for
    import tkinter as tk    # either in python 2 or in python 3
    import tkinter.ttk as ttk
except ImportError:
    import Tkinter as tk
    import ttk


def upon_var_change(var):
    print(var.get())


if __name__ == '__main__':
    root = tk.Tk()
    cbs = list()
    for i in range(3):
        cbs.append(ttk.Combobox(root))
        cbs[i].var = tk.StringVar()
        cbs[i].var.trace_add('write', lambda *_,
                                        var=cbs[i].var:upon_var_change(var))
        cbs[i]['textvariable'] = cbs[i].var
        cbs[i].grid(row=i // 2, column=i % 2, sticky='nw')
    tk.mainloop()

If such is required you could identify the Variable class from its internal reference as well:

try:                        # In order to be able to import tkinter for
    import tkinter as tk    # either in python 2 or in python 3
    import tkinter.ttk as ttk
except ImportError:
    import Tkinter as tk
    import ttk


def upon_var_change(var_name):
    value = root.tk.globalgetvar(var_name)
    print(var_name, value)


if __name__ == '__main__':
    root = tk.Tk()
    cbs = list()
    for i in range(3):
        cbs.append(ttk.Combobox(root))
        cbs[i].var = tk.StringVar()
        cbs[i].var.trace_add('write', lambda var_name,
                                                *_: upon_var_change(var_name))
        cbs[i]['textvariable'] = cbs[i].var
        cbs[i].grid(row=i // 2, column=i % 2, sticky='nw')
    tk.mainloop()

Upvotes: 5

Related Questions