用于小部件或使用.trace_variable()技术的事件值变更类型? [英] On-value-change type of event for widgets or use .trace_variable() technique?

查看:149
本文介绍了用于小部件或使用.trace_variable()技术的事件值变更类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有数据输入小部件(如输入,文本,微调,按钮,单选按钮)的值更改类型的事件?通过值更改,我的意思是能够通过键盘输入或剪切/删除/粘贴(和Text edit_undo / edit_redo)活动来检测小部件的值是否发生变化?我看不到Tkinter事件文档[1]中描述的这样的事件。

Is there an on-value-change type of event for data input widgets like Entry, Text, Spinner, Checkbutton, Radiobutton? By on-value-change, I mean the ability to detect when the value of a widget has changed due to keyboard input or cut/delete/paste (and Text edit_undo/edit_redo) activity? I see no such event described in the Tkinter event documentation [1].

将Tkinter变量链接到窗口小部件值的正确技术我想要监视并使用这些变量' .trace_variable('w',...)方法绑定到值更改?这似乎是正确的方法,但是在我研究的Tkinter应用程序源代码中,我没有看到很多trace_variable()使用,导致我对使用这种方法要谨慎。

Is the proper technique to link Tkinter variables to widget values I want to monitor and use these variables' .trace_variable( 'w', ... ) methods to bind to value changes? This seems like the right approach, but I haven't seen a lot of trace_variable() use in the Tkinter application source code that I've studied ... leading me to be cautious about using this approach.

[1] http:// infohost .nmt.edu / tcc / help / pubs / tkinter / events.html

推荐答案

不同的小部件要求不同的解决方案。例如,检查按钮和单选按钮具有命令选项,并且使用条目小部件可以使用内置的验证功能。

Different widgets call for different solutions. For example, check buttons and radio buttons have a command option, and with an entry widget you can use the built-in validation features.

对于可以绑定到变量的所有小部件,执行变量跟踪是一个常见的解决方案。文本小部件是一个例外,因为您无法将其与变量相关联,而无需大量工作。

For all the widgets that can be tied to a variable, doing a variable trace is a common solution. The text widget is one exception since you can't associate it with a variable without a lot of effort.

在tcl / tk世界中,我将所有的小部件都关联到单个数组(tcl的哈希映射/字典名称),然后在数组上放置一个跟踪。不幸的是,tkinter不直接支持tcl数组。但是,支持有点容易入侵。有关更多信息,请参阅我对此问题的回复:每当Tkinter小部件值更改时,如何运行代码?

In the tcl/tk world I associate all my widgets to a single array (tcl's name for a hash map / dictionary) and then put a single trace on the array. Unfortunately tkinter doesn't directly support tcl arrays. However, support is somewhat easy to hack in. For more information see my response to this question: How to run a code whenever a Tkinter widget value changes?

这篇关于用于小部件或使用.trace_variable()技术的事件值变更类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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