Python Tkinter 变量跟踪方法回调的参数是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29690463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:53:15  来源:igfitidea点击:

What are the arguments to Tkinter variable trace method callbacks?

pythontkinter

提问by ToothlessRebel

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

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

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

例如,BooleanVar()这些似乎是'', 'PYVAR0', 'w'.

The third argument xappears 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?

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

采纳答案by Bryan Oakley

The first argumentis the internal variable name. You can use this name as an argument to the tkinter getvarand setvarmethods. 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)

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

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

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

The third argumentis 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")

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

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

回答by Keith Caulfield

The first argument is the name of the variable, but is not "useless" since you can set it when you declare the variable, e.g.:

第一个参数是变量的名称,但不是“无用的”,因为您可以在声明变量时设置它,例如:

someVar = IntVar(name="Name of someVar")

When you check the first argument in the trace callback it will equal "Name of someVar". Using the name to distinguish between variables, you can then bind the same handler to trace changes to any number of variables, rather than needing a separate handler for each variable.

当您检查跟踪回调中的第一个参数时,它将等于“someVar 的名称”。使用名称来区分变量,然后您可以绑定相同的处理程序来跟踪对任意数量变量的更改,而不需要为每个变量单独使用一个处理程序。