Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Tkinter: How to "break" class-event-bindings from within tag-bindings?

29 views
Skip to first unread message

Michael Schwarz

unread,
Sep 9, 2002, 9:01:16 AM9/9/02
to
A "break" as the return value of a tag-event-binding for the
Text-widget seems not to prevent the standard event-handling of the
widget to occur.

This seems to be an error!

How can it be done another (elegant) way?

See the following example:

===

# How can tag-events "break" class-bindings?
from Tkinter import *

def Jump(e):
# Sprung:
targetFrom, targetTo = "3.5", "3.9"
F.Text.tag_remove("sel", "0.0", "end")
F.Text.tag_add("sel", targetFrom, targetTo)
F.Text.mark_set("insert", targetFrom)
return "break" # Doesn't do it !!!

F = Tk()
F.Text = Text(F) # , width=120, height=35, bg="white")
F.Text.pack() # (side="right", fill="both", expand=1)
F.Text.focus_set()
F.Text.insert(INSERT, """\
Link

Jump HERE
""")
F.Text.tag_bind("link", "<1>", Jump)
F.Text.tag_config("link", underline = "on")
F.Text.tag_add("link", "1.0", "1.4")

# Comment the following line and the jump won't do it anymore
# due to the standard class-bindings of the text-widget
F.Text.bind("<1>",lambda e: "break")

F.mainloop()

Jeff Epler

unread,
Sep 9, 2002, 9:57:30 AM9/9/02
to
On Mon, Sep 09, 2002 at 03:01:16PM +0200, Michael Schwarz wrote:
> A "break" as the return value of a tag-event-binding for the
> Text-widget seems not to prevent the standard event-handling of the
> widget to occur.

I can reproduce this in a bare "wish" script, without involving Python.
I tested on wish8.4 (somewhat stale CVS) and 8.3.3.

The manpage for text(n) says this:
It is possible for the current character to have multiple tags,
and for each of them to have a binding for a particular
event sequence. When this occurs, one binding is invoked for
each tag, in order from lowest- priority to highest priority.
If there are multiple matching bindings for a single tag, then the
most specific binding is chosen (see the manual entry for the
bind command for details). continue and break commands within
binding scripts are processed in the same way as for bindings
created with the bind command.

If bindings are created for the widget as a whole using the bind
command, then those bindings will supplement the tag bindings.
The tag bindings will be invoked first, followed by bindings for
the window as a whole.
I'm not sure if this is the intended behavior or not. After all,
continue and break are supposed to be processed 'in the same way as'
normal bindings. However, maybe the word "supplement" and the description
"followed by bindings for the window as a whole" are supposed to describe
the behavior you saw as correct.

You may wish to file a bug against tk or post in a tk newsgroup
(comp.lang.tcl) to ask whether this behavior is correct or is a bug.

My "wish" script to show the problem:

text .t
.t insert end "Link\n\nJump HERE"
.t tag add link 1.0 1.4
.t tag bind link <1> { jump; break }
.t tag config link -underline on -foreground blue

#bind .t <1> { break }

proc jump {} {
.t tag remove sel 0.0 end
.t tag add sel 3.5 3.9
}

pack .t

0 new messages