关于 tkinter 中的绑定标签的基本查询 [英] Basic query regarding bindtags in tkinter

查看:29
本文介绍了关于 tkinter 中的绑定标签的基本查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章的给定示例中,提到如果使用默认绑定标签,则事件值将不会可见内部定义(会有一个滞后).

有一些关于类绑定的解释.
我是初学者,所以想了解详细原因.有人可以解释为什么它在第一种情况下不起作用而在第二种情况下起作用(当修改了 bindtags 的顺序时).

import Tkinterdef OnKeyPress(事件):值 = event.widget.get()string="%s 的值是 '%s'" % (event.widget._name, value)状态.配置(文本=字符串)根 = Tkinter.Tk()entry1 = Tkinter.Entry(root, name="entry1")entry2 = Tkinter.Entry(root, name="entry2")entry3 = Tkinter.Entry(root, name="entry3")entry1.bindtags(('.entry1', 'Entry', '.', 'all'))entry2.bindtags(('Entry', '.entry1', '.', 'all'))entry3.bindtags(('.entry1','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")entry1.bind("", OnKeyPress)entry2.bind("", OnKeyPress)entry3.bind_class("post-class-bindings", "", OnKeyPress)root.mainloop()

解决方案

在小部件上进行绑定时,实际上并没有绑定到小部件本身.当您执行 mywidget.bind(...) 时,实际发生的是绑定与一个与小部件同名的 bind 标签 相关联.>

当检测到事件时,Tkinter 首先确定哪个小部件拦截了该事件.此小部件将具有与之关联的零个或多个(默认情况下:四个)绑定标签的列表.Tkinter 将检查每个标签以查看是否存在与事件匹配的绑定.如果找到,它将执行绑定,然后继续下一个标记,直到用完标记或绑定函数之一返回字符串 "break".

序列看起来像这样:

  1. 您按下x"键.此时x"尚未插入任何地方
  2. Tkinter 获取具有键盘焦点的小部件的绑定标签.
  3. 默认情况下,第一个绑定标签是小部件本身.那个标签上有绑定吗?如果是,请执行它.例如,您可以打印小部件的内容.由于尚未触发其他绑定,因此x"不会成为内容的一部分.
  4. 如果绑定函数返回 "break" 则不再进行事件处理.x"不会被插入到小部件中.
  5. 如果小部件没有返回 "break",tkinter 将继续下一个绑定标签.
  6. 默认情况下,下一个绑定标签是小部件类.类上是否存在与此事件匹配的绑定(按下字母x"的按键)?
  7. 在这种情况下,该类确实具有此事件的绑定,即实际将x"插入到小部件中.直到此时,x"仍未插入小部件.但是,在此绑定之后,如果您打印内容,您会看到x"
  8. 以这种方式继续处理,直到处理完所有绑定标签.

根据评论部分正在进行的讨论,这似乎仍然不清楚.我会尽量简化:

它是类绑定,它将字符从内存中的事件对象复制到小部件,从而使其出现在屏幕上.在类绑定触发之前,角色不会出现在小部件中.在类绑定之后,它将在小部件中.

In the given example from this post, it was mentioned that if default bindtags are used then event value will not be visible inside definition (there will be lag by one).

There was some explanation regarding class binding.
I am a beginner, so would like to understand the detailed reason.
Can some please explain why it was not working in first case and was working in second case (when order of bindtags is modified).

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")

entry1.bindtags(('.entry1', 'Entry', '.', 'all'))
entry2.bindtags(('Entry', '.entry1', '.', 'all'))
entry3.bindtags(('.entry1','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")
entry1.bind("<KeyPress>", OnKeyPress)
entry2.bind("<KeyPress>", OnKeyPress)
entry3.bind_class("post-class-bindings", "<KeyPress>", OnKeyPress)

root.mainloop()

解决方案

When you do a binding on a widget, you aren't actually binding to a widget per se. When you do mywidget.bind(...), what is actually happening is that the binding is associated with a bind tag with the same name as the widget.

When an event is detected, Tkinter first figures out which widget intercepted the event. This widget will have a list of zero or more (by default: four) bind tags associated with it. Tkinter will check each tag in order to see if there's a binding that matches the event. If it finds one, it will execute the binding and then continue to the next tag, until it runs out of tags or one of the bound functions returns the string "break".

The sequence looks something like this:

  1. You press the "x" key. At this point the "x" hasn't been inserted anywhere
  2. Tkinter gets the bind tags for the widget that has the keyboard focus.
  3. By default the first bind tag is the widget itself. Is there a binding on that tag? If so, execute it. For example, you might print the contents of the widget. Because no other bindings have fired yet, the "x" will not be part of the contents.
  4. If the bound function returns "break" then no more event processing is done. The "x" will not get inserted into the widget.
  5. If the widget did not return "break", tkinter proceeds to the next bind tag.
  6. By default, the next bind tag is the widget class. Is there a binding on the class that matches this event (a keypress of the letter "x")?
  7. In this case the class does have a binding for this event, which is to actually insert the "x" into the widget. Until this point, the "x" still hasn't been inserted into the widget. After this binding, however, if you did a print of the contents you would see the "x"
  8. Processing continues in this manner until all bind tags have been processed.

Based on the ongoing discussion in the comment section, it appears this is still unclear. I'll try to make this as simple as possible:

It is the class binding which copies a character from the in-memory event object to the widget and thus causing it to appear on screen. Before the class binding fires, the character will not appear in the widget. After the class binding it will be in the widget.

这篇关于关于 tkinter 中的绑定标签的基本查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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