结合<关键>在Tkinter的条目 [英] Binding <Key> to an Entry in Tkinter

查看:113
本文介绍了结合<关键>在Tkinter的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的事件绑定条目和阅读的内容,改变以某种方式落后。我想动态更新另一个条目,一旦条目1改变显示的各个项的内容的计算的结果。但不知何故的变化不会立即承认,只在前述之一。唐吨知道,如果这个问题是清楚的:
如果我从N更改,更改到N-1的认可。如果号码是1000,我preSS退格键两次,entry_1.get()将产生100,而不是10,希望你明白我的意思了:)

When I bind the event to an entry and read the content, the change somehow lags behind. I want to "dynamically update" another entry that shows the result of a calculation of the contents of various entries as soon as entry 1 is changed. But somehow the change is not recognized instantly, only the foregoing one. Don t know if the problem is clear: If I make n changes, the changes up to n-1 are recognized. If the number was 1000 and I press backspace twice, entry_1.get() would yield 100 instead of 10. Hope you understand what i mean now :)

code段(简体):

Code snippet (simplified):

self.entry_1.bind('<Key>',lambda d: self.update())

def update(self):
    success=True
    try:
        float(self.entry_1.get())
        float(self.entry_1.get())
    except ValueError: success=False
    if success:

        self.entry_3.delete(0,"end")
        x=(float(self.entry_1.get())*float(self.entry_2.get())
        self.entry_3.insert("end", "%g" %x)

什么可能是什么原因呢?

What might be the reason for that?

推荐答案

究其原因是由于事件处理顺序。该顺序由小部件的绑定标记(或bindtag)定义。默认情况下,顺序是小部件,类,顶层,全部。例如,如果你有一个widget的结合,并在类,以及包含该插件的顶层窗口,并在特殊情况全部,绑定将触发的顺序。

The reason is due to the order that events are processed. That order is defined by the "binding tag" (or bindtag) of the widget. By default the order is widget, class, toplevel, "all". For example, if you have a binding on the widget, and on the class, and on the toplevel window that contains the widget, and on the special case "all", the bindings will fire in that order.

我给了这个问题一个冗长的书面记录在<一个href=\"http://stackoverflow.com/questions/3501849/how-to-bind-self-events-in-tkinter-text-widget-after-it-will-binded-by-text-widge/3513906#3513906\">this回答的问题如何绑定在Tkinter的文本小部件自事件后,它将被文本控件绑定?

I gave a lengthy writeup of this problem in this answer to the question How to bind self events in Tkinter Text widget after it will binded by Text widget?

这篇关于结合&LT;关键&GT;在Tkinter的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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