python gui事件乱序 [英] python gui events out of order

查看:20
本文介绍了python gui事件乱序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from Tkinter import *
from tkMessageBox import *

class Gui:
 def __init__(self, root):
  self.container = Frame(root)
  self.container.grid()
  self.inputText = Text(self.container, width=50, height=8)
  self.outputText = Text(self.container, width=50, height=8, bg='#E0E0E0', state=DISABLED)
  self.inputText.grid(row=0, column=0)
  self.outputText.grid(row=0, column=1)

  self.inputText.bind("<Key>", self.translate)

 def translate(self, event):
  input  = self.inputText.get(0.0, END)
  output = self.outputText.get(0.0, END)

  self.outputText.config(state=NORMAL)
  self.outputText.delete(0.0, END)
  self.outputText.insert(INSERT, input)
  self.outputText.config(state=DISABLED)

  showinfo(message="Input: %s characters
Output: %s characters" % (len(input), len(input)))


root = Tk()   #toplevel object
app = Gui(root)  #call to the class where gui is defined
root.mainloop()  #enter event loop

在 tkinter 中处理 gui 我对事件处理程序的运行顺序有些困惑.如果你运行上面的代码,你会希望看到...

Working on a gui in tkinter I'm a little confused as to the sequence the event handlers are run. If you run the above code you'll hopefully see...

1) 编辑文本小部件会触发事件处理程序,但它似乎在不注册实际更改的情况下触发它,2)即使文本小部件被清除(即,继续按 BackSpace)它似乎仍然有一个字符长度的字符串,3) 尽管数据来自前一个事件,但输出小部件仅在 NEXT 事件触发器被触发时接收其更新.

1) Editing the text widget triggers the event handler but it seems to fire it off without registering the actual change, 2) Even when the text widget is cleared (ie, keep pressing BackSpace) it still seems to have a one character length string, 3) The output widget only receives its update when the NEXT event trigger is fired despite the fact the data came on the previous event.

这就是绑定在 tkinter 中的工作方式还是我在这里遗漏了什么?

Is this just how bindings work in tkinter or am i missing something here?

更新输入小部件时我想要的行为是:1)显示变化,2) 输入事件处理程序,3)更新输出小部件,4) 显示消息框.

The behaviour i would like when updating the input widget is: 1) Show the change, 2) Enter event handler, 3) Update output widget, 4) Show message box.

推荐答案

这就是绑定的工作方式(这是一件好事),但您的问题很容易解决.

This is how bindings work (and that's a good thing), but your problem is easily solved.

绑定按小部件绑定标签(也称为绑定标签或绑定标签)指定的顺序触发.除非您另行指定,否则绑定将按以下顺序进行:

Bindings are fired in the order specified by a widgets binding tags (also known as bind tags or bindtags). Unless you specify otherwise, the bindings happen in the following order:

  1. 如果小部件上有直接绑定,它将在任何其他绑定之前被触发.
  2. 如果小部件的类上有绑定,则接下来会触发它
  3. 如果在包含该小部件的顶级小部件上存在绑定,则接下来会触发它(注意:在此上下文中,根窗口被视为顶级窗口)
  4. 如果all"上有绑定,接下来会触发.

一个事件处理程序可以在任何时候停止该序列,但这不是本次具体讨论的重点.

The sequence can be stopped at any point by one of the event handlers, but that's beside the point for this specific discussion.

在默认情况下,您对 的绑定发生在类绑定之前,并且在类绑定中文本实际插入到小部件中.这就是为什么您的绑定似乎总是落后一个字符.

In the default case, your binding on <Key> happens before the class binding, and it is the class binding where the text is actually inserted into the widget. That is why your binding always seems to be one character behind.

通常这种顺序是完全正确的,因为更具体的绑定有机会覆盖默认行为.如果不是这样,即使您不想要它,您也总是会得到默认行为.这并不总是您想要的一个地方是当您想要增加默认绑定而不是替换它们时.

Normally this order of things is exactly right, since more specific bindings get a chance to override the default behavior. If it wasn't this way you'd always get the default behavior even if you didn't want it. One place where this is not always what you want is when you want to augment the default bindings rather than replace them.

您可以交换绑定标签的顺序,以便首先进行类绑定.或者,向您的文本小部件添加一个额外的绑定标签,并将其添加到类绑定之后的序列中,然后绑定到它.通常添加绑定标签是更好的解决方案,但并非总是如此.

You can swap the order of the bindtags so that the class binding happens first. Or, add an additional bindtag to your text widget and add that in the sequence after the class binding, and bind to that. Usually adding a bindtag is the better solution, but not always.

要更改绑定标签,您可以执行以下操作:

To change the bindtags you can do something like this:

self.inputText.bindtags(((str(self.inputText)), "Text", "post-insert", ".", "all"))

要绑定到后插入",请使用 bind_class 方法:

To bind to "post-insert", do it with the bind_class method:

self.inputText.bind_class("post-insert", "<Key>", self.translate)

这可能看起来很奇怪,但绑定标签是目前最强大的绑定机制之一.它们使您可以完全和完全控制绑定的顺序,这对于任何其他工具包来说要困难得多.

It may seem odd, but bindtags are one of the most powerful binding mechanisms out there. They give you complete and total control over the order of bindings, which is much more difficult with any other toolkit.

顺便说一句,不要忘记,如果您将所有字符都放到文本小部件的末尾,那么末尾总会有一个额外的换行符.要么转到 end-1c,要么从文本中剪掉一个换行符.

By the way, don't forget that if you get all the characters to the end of the text widget there will always be an extra newline at the end. Either get to end-1c, or trim off one newline from the text.

这篇关于python gui事件乱序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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