如何绑定在Tkinter的文本小部件自事件后,它将被文本控件绑定? [英] How to bind self events in Tkinter Text widget after it will binded by Text widget?

查看:215
本文介绍了如何绑定在Tkinter的文本小部件自事件后,它将被文本控件绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要绑定文本 widget类绑定后的自我活动,为了改变widget的文字时,我结合函数被调用。我的绑定,例如 self.text.bind(<关键>中,self.callback)。,在文本控件的内容更改前名为

I want to bind self events after Text widget class bindings, in order to change the text of the widget when my binding function is called. My binding, for example self.text.bind("<Key>", self.callback), is called before the content in Text widget changes.

推荐答案

什么是你的情况发生的是,你的绑定打印类结合之前的值发生,它是类结合实际需要用户输入,并把它在窗口小部件。有几种方法来解决这个问题。你可以绑定到&LT; KeyRelease&GT; 而不是&LT;关键preSS&GT; ,或者你可以使用内置入境验证功能,让你的code呼吁每一个关键preSS。随着你会得到你需要的所有数据的解决方案 - 在更改前的数值,变更后的价值,这是pssed $ P $钥匙等

What is happening in your case is that your binding to print the value happens before the class binding, and it's the class binding that actually takes user input and puts it in the widget. There are several ways to solve this problem. You could bind to <KeyRelease> instead of <KeyPress>, or you could use the built-in entry validation features to have your code called on every key press. With that solution you'll be given all the data you need -- the value before the change, the value after the change, the key that was pressed, etc.

另一种选择是,以更改事件被处理的顺序。由于您的问题具体问到如何改变顺序,这也是我将解决。

Another choice is to change the order in which events are processed. Since your question specifically asked how to change the order, that is what I will address.

虽然有约束力的出现与当你做这样的事情 entry.bind一个小部件相关联(...),你实际上分配绑定到绑定标签(或bindtag)。默认每个插件具有bindtag是一样的部件的名称。其他bindtags包括类小窗口(例如,输入),根窗口的路径(例如:。)和特殊标记所有。当接收到事件的小部件被分配了一组被处理bindtags的顺序。默认顺序去从most-到特殊至少:小部件,阶级,顶层,所有的。

Even though a binding appears to be associated with a widget when you do something like entry.bind(...), you're actually assigning a binding to a "bind tag" (or "bindtag"). By default each widget has a bindtag that is the same as the name of the widget. Other bindtags include the class of a widget (for example, "Entry"), the path of the root window (eg: ".") and the special tag "all". Widgets are assigned a set of bindtags which are processed in order when an event is received. The default order goes from most- to least-specific: widget, class, toplevel, all.

有几种方法来操纵bindtags得到你想要的结果。一个选择是重新排列bindtags的顺序。通过移动重新presents小部件是bindtag重新presenting课后bindtag,该类将其传递到特定的部件之前处理该事件。

There are a couple ways to manipulate the bindtags to get the result you desire. One choice is to rearrange the order of the bindtags. By moving the bindtag that represents the widget to be after the bindtag representing the class, the class will handle the event before passing it on to the specific widget.

另一种选择是在这个标签添加一个额外的bindtag是后级绑定,然后把你的绑定,而不是上重新presents该插件的标签。

Another choice is to add an additional bindtag that is after the class binding, and then put your bindings on this tag rather than on the tag that represents the widget.

为什么选择了另一种?通过重新排列顺序,你会影响在该部件所有绑定。如果你有很多的绑定以及一些依赖的顺序(这样就可以了,例如,禁止某些按键),改变顺序可能会导致这些绑定停止工作。

Why choose one over the other? By rearranging the order you will affect all bindings on that widget. If you have many bindings and some depend on the order (so that the can, for example, disallow certain keystrokes), changing the order may cause those bindings to stop working.

通过引入新的bindtag,您可以选择以后的事情类之前绑定绑定其发生和。

By introducing a new bindtag, you can choose which bindings happen before class bindings and which happen after.

在以下code创建三个入口小部件。第一种方法使用bindtags的默认设定(本例中明确设置,尽管它们是相同的默认值)。第二改变顺序,以及第三引入附加bindtag。运行code,那么preSS的关键,而重点是在每个窗口。注意,在第一项小部件总是结合似乎是后面一个字符。再次,这是因为微件结合发生在类结合放字符到插件之前

In the following code I create three entry widgets. The first uses the default set of bindtags (explicitly set in the example, though they are identical to the default). The second changes the order, and the third introduces an additional bindtag. Run the code then press a key while the focus is in each window. Notice that in the first entry widget the binding always seems to be one character behind. Again, this is because the widget binding happens before the class binding puts the character into the widget.

在第二和第三个实施例中,结合发生在类装订因此函数看到在小部件后的变化。

In the second and third examples, the binding happens after the class binding so the function sees the change in the widgets.

import Tkinter

def OnKeyPress(event):
    value = event.widget.get()
    string="value of %s is '%s'" % (event.widget._name, value)
    status.configure(text=string)

root = Tkinter.Tk()

entry1 = Tkinter.Entry(root, name="entry1")
entry2 = Tkinter.Entry(root, name="entry2")
entry3 = Tkinter.Entry(root, name="entry3")

# Three different bindtags. The first is just the default but I'm
# including it for illustrative purposes. The second reverses the
# order of the first two tags. The third introduces a new tag after
# the class tag.
entry1.bindtags(('.entry1', 'Entry', '.', 'all'))
entry2.bindtags(('Entry', '.entry2', '.', 'all'))
entry3.bindtags(('.entry3','Entry','post-class-bindings', '.', 'all'))

btlabel1 = Tkinter.Label(text="bindtags: %s" % " ".join(entry1.bindtags()))
btlabel2 = Tkinter.Label(text="bindtags: %s" % " ".join(entry2.bindtags()))
btlabel3 = Tkinter.Label(text="bindtags: %s" % " ".join(entry3.bindtags()))
status = Tkinter.Label(anchor="w")

entry1.grid(row=0,column=0)
btlabel1.grid(row=0,column=1, padx=10, sticky="w")
entry2.grid(row=1,column=0)
btlabel2.grid(row=1,column=1, padx=10, sticky="w")
entry3.grid(row=2,column=0)
btlabel3.grid(row=2,column=1, padx=10)
status.grid(row=3, columnspan=2, sticky="w")

# normally you bind to the widget; in the third case we're binding
# to the new bindtag we've created
entry1.bind("<KeyPress>", OnKeyPress)
entry2.bind("<KeyPress>", OnKeyPress)
entry3.bind_class("post-class-bindings", "<KeyPress>", OnKeyPress)

root.mainloop()

这篇关于如何绑定在Tkinter的文本小部件自事件后,它将被文本控件绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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