如何确保在FocusOut上保存tkinter条目的内容?

时间:2016-10-14 11:19:18

标签: python tkinter focusout tkinter-entry

我有一个使用<FocusOut>绑定的应用自动将Entry中的修改保存到列表中。

使用Entry导航条目或单击其他条目时保存TAB文本没有问题,但如果我在一个条目上更改文本,然后如果我鼠标 - 单击另一个框架中的ListBox<FocusOut>对最后选择的条目不起作用,并且其中的信息未注册。

如果不借助GUI上的Save按钮,如何避免这种情况?对于ListBox中的每个选项,都有不同的Entry框,因此用户必须多次按下Save按钮。我想避免这种情况。

1 个答案:

答案 0 :(得分:1)

实时编辑/保存文本

看起来您希望实时获取更新的文本。在这种情况下我做的是使用'KeyRelease' - 绑定。简单,有效的enrtry特定,并立即工作。

概念:

win = Tk()
def dosomething(*args):
    # update the corresponding text anywhere, save your text, whatever
    print(entry.get())

entry = Entry()
entry.bind("<KeyRelease>", dosomething)
entry.pack()

win.mainloop()

enter image description here

行动中:

M
Mo
Mon
Monk
Monke
Monkey
Monkey 
Monkey e
Monkey ea
Monkey eat
Monkey eats
Monkey eats 
Monkey eats b
Monkey eats ban
Monkey eats ban
Monkey eats bana
Monkey eats banan
Monkey eats banana