本文介绍了Tkinter,在调用 <Key> 之前更新 Entry 小部件捆绑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,每次将密钥输入到 tkinter.Entry 小部件设置时都会更新,如下所示:

I have a script that updates everytime a key is entered into a tkinter.Entry widget setup like so:

self.entrySearch.bind("<Key>", self.updateSearch)

我遇到的问题是,我绑定到 "" 的方法在将密钥输入到 Entry 小部件之前已解决.这意味着当我调用 self.entrySearch.get() 时,我只能得到最后一次击键之前输入框中的内容.

The problem that I am having is that the method I've bound to "<Key>" is resolved before the key is entered into the Entry widget. This means that when I call self.entrySearch.get(), I only get what was in the Entry box just before the last keystroke.

我试过简单地将字符附加到末尾,但我想不出解决退格或删除的方法,或者字符在字符串中间输入的位置.

I've tried simple appending the character onto the end, but I can't think of a way to resolve Backspaces or Deletes, or where the character is entered mid-string.

基本上,我正在寻找一种方法,允许在我的绑定解决之前更新输入框.

Basically, what I'm looking for is a method to allow the entry box to update before my binding is resolved.

谢谢.

推荐答案

将条目绑定到 变量.跟踪变量变化.

Bind the entry to a variable. Trace the variable change.

try:
    from Tkinter import *
except ImportError:
    from tkinter import *

def print_entry_value(*args):
    print(v.get())

root = Tk()
v = StringVar()
v.trace('w', print_entry_value)
e = Entry(root, textvariable=v)
e.pack()
root.mainloop()

这篇关于Tkinter,在调用 &lt;Key&gt; 之前更新 Entry 小部件捆绑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

查看更多