Tkinter 变量跟踪方法回调的参数是什么? [英] What are the arguments to Tkinter variable trace method callbacks?

查看:58
本文介绍了Tkinter 变量跟踪方法回调的参数是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 有 Tkinter 变量的类 StringVar()BooleanVar() 等.这些都共享方法 get()set(string)trace(mode, callback).callback 函数作为第二个参数传递给 trace(mode, callback) 传递了四个参数,self, n, m, x.

Python has classes for Tkinter variables StringVar(), BooleanVar(), etc. These all share the methods get(), set(string), and trace(mode, callback). The callback function passed as the second argument to trace(mode, callback) is passed four arguments, self, n, m, x.

对于 BooleanVar() 的示例,它们看起来是 '', 'PYVAR0', 'w'.

For an example of a BooleanVar() these appear to be '', 'PYVAR0', 'w'.

第三个参数 x 似乎是触发跟踪的模式,在我的情况下,变量已更改.但是,第一个看起来是空字符串的变量是什么?第二个是什么,如果我不得不猜测我会说变量的一些内部名称?

The third argument x appears to be the mode that triggered the trace, in my case the variable was changed. However, what is the first variable that appears to be an empty string? What is the second, if I had to guess I'd say some internal name for the variable?

推荐答案

第一个参数是内部变量名.您可以将此名称用作 tkinter getvarsetvar 方法的参数.如果您给变量命名(例如:StringVar(name='foo')),这将是给定的名称,否则它将是 tkinter 为您生成的名称(例如:PYVAR0)

The first argument is the internal variable name. You can use this name as an argument to the tkinter getvar and setvar methods. If you give your variable a name (eg: StringVar(name='foo')) this will be the given name, otherwise it will be a name generated for you by tkinter (eg: PYVAR0)

如果第一个参数表示一个列表变量(在 tkinter 中极不可能),第二个参数 将是该列表的索引.如果是标量变量,第二个参数将为空字符串.

If the first argument represents a list variable (highly unlikely in tkinter), the second argument will be an index into that list. If it is a scalar variable, the second argument will be the empty string.

第三个参数是操作,如果您使用相同的方法读取、写入和/或删除变量,则很有用.这个参数告诉你哪个操作触发了回调.写操作(设置变量)为w",读操作(获取变量的值)为r".如果变量被删除,它将是u"(u"是unset"的缩写)

The third argument is the operation, useful if you are using the same method for reading, writing and/or deleting the variable. This argument tells you which operation triggered the callback. It will be "w" for a write operation (setting the variable) and "r" for a read operation (getting the value of the variable). It will be "u" if the variable is deleted ("u" is short for "unset")

Tkinter 是一个围绕 tcl/tk 解释器的 Python 包装器.可以在此处找到有关变量跟踪的权威文档:http://tcl.tk/man/tcl8.5/TclCmd/trace.htm#M14.虽然,这仅记录了内部跟踪的工作方式,但 tkinter 包装器有时会处理数据.

Tkinter is a python wrapper around a tcl/tk interpreter. The definitive documentation for variable traces can be found here: http://tcl.tk/man/tcl8.5/TclCmd/trace.htm#M14. Though, this only documents how the internal trace works, the tkinter wrapper sometimes massages the data.

这篇关于Tkinter 变量跟踪方法回调的参数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆