Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

python - Basic query regarding bindtags in tkinter

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()
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...