Python: How to get an updated Entry text to use in a command binded to it?

て烟熏妆下的殇ゞ 提交于 2019-12-02 04:28:22

You could replace the <Key> event with the <KeyRelease> event. That should work.

Here is a list of events: http://infohost.nmt.edu/tcc/help/pubs/tkinter/events.html#event-types

Bryan Oakley

The reason for this has to do with Tk "bindtags". Bindings are associated with tags, and the bindings are processed in tag order. Both widget names and widget classes are tags, and they are processed in that order (widget-specific bindings first, class bindings second).

For that reason, any time you press a key your widget specific binding will fire before the class binding has a chance to modify the widget.

There are many workarounds. The simplest is to bind to <KeyRelease> since the class bindings happen on the key press. There are other solutions that involve either adding or rearranging bind tags, or using the built-in data validation features of the entry widget. Which is best depends on what you're really trying to accomplish.

For more information on the data validation functions, see this question: Interactively validating Entry widget content in tkinter

For a more comprehensive answer, see Tkinter: set StringVar after <Key> event, including the key pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!