绑定<键>到 Tkinter 中的条目

发布于 2024-12-02 02:07:56 字数 674 浏览 1 评论 0原文

当我将事件 绑定到条目并读取内容时,更改在某种程度上滞后了。我想“动态更新”另一个条目,该条目显示条目 1 更改后各个条目内容的计算结果。但不知何故,这种变化并没有立即被识别出来,只有前述的变化。不知道问题说清楚了没有,这么说吧: 如果我进行 n 次更改,则最多可识别 n-1 次更改。例如,如果条目中包含数字 1000,并且我按两次退格键,entry_1.get() 将生成 100 而不是 10。希望您现在明白我的意思 :)

代码片段(简化):

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

def update(self):
    success=True
    try:
        float(self.entry_1.get())
        float(self.entry_2.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)

可能是什么原因那?

When I bind the event <Key> 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, so let's say it like this:
If I make n changes, the changes up to n-1 are recognized. For example, if the number 1000 is in the entry and I press backspace twice, entry_1.get() would yield 100 instead of 10. Hope you understand what i mean now :)

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_2.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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2024-12-09 02:07:56

原因在于事件的处理顺序。该顺序由小部件的“绑定标签”(或bindtag)定义。默认情况下,顺序是小部件、类、顶级、“全部”。例如,如果您在小部件、类、包含小部件的顶层窗口以及特殊情况“all”上有绑定,则绑定将按该顺序触发。

我在 这个答案问题如何在 Tkinter 文本小部件中绑定自身事件后被文本小部件绑定?

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.

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文