简体   繁体   English

如何访问触发跟踪回调的特定 tkinter StringVar?

[英]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?有没有办法获得对触发跟踪回调的实际变量的引用或使用内部名称(如 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.尝试调用 self.get() 失败,因为 self 只是那个字符串表示,而不是实际的 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.我不想为每个 StringVar 设置一个特定的回调,因为实际接口有大约 30 个框,我都希望通过相同的标准进行验证。

You could simply pass the arguments you want by making use of lambda statement for anonymous functions.您可以通过将lambda语句用于匿名函数来简单地传递您想要的参数。 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.对于问题中的示例,我认为list应该很合适。

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:如果需要,您也可以从其内部引用中识别 Variable 类:

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

I know this is an old post, gut I found a way to use the internal variable name.我知道这是一篇旧帖子,我找到了一种使用内部变量名称的方法。 Modify your validate function like this:像这样修改你的validate函数:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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