textwidget.tag_bind("name", "<Any-KeyPress>", self.donothing) not working

S

shannonl

Hi all,

For some reason this bind is calling the donothing function, like it
should, but is then allowing the text to be inserted into the Text
widget.

Here is the code:

self.framebody.tag_config("name", underline=1)
self.framebody.tag_bind("name", "<Any-KeyPress>", self.donothing)

line = '\t%s' % ('sometext')
self.framebody.insert(END, line, "name")

def donothing(self):
return 'break'

This donothing function should keep the tcl bind from inserting the
text...at least so I thought.

This bind does work on the text widget as a whole, but on a individual
tag, it does not.

Can anyone help me out with this?

Thanks in advance,
Shannon
 
J

jepler

I'm not sure why Tk behaves this way, but you can observe the same behavior
with a "wish" script. It would seem that "break" skips any other scripts
associated with item tags, but still proceeds to bindings at the widget level.
Using a binding with "break" on the canvas itself may help you get the behavior
you want.

########################################################################
canvas .c -width 70 -height 70
set it [.c create text 20 20 -tags x -text example -anchor nw]
..c bind x <KeyPress> { puts "item binding"; break; }
bind .c <KeyPress> { puts "widget binding"; break }
bind . <KeyPress> { puts "toplevel binding"; break }

pack .c
focus .c
..c focus $it
########################################################################

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDXpdyJd01MZaTXX0RArf7AJkBHDwABPgHjOtspieasTiC+yxSUACfQJIk
EGk98dqUPvBKtycoRYpPmH4=
=fa5/
-----END PGP SIGNATURE-----
 
S

Steve Holden

shannonl said:
Hi all,

For some reason this bind is calling the donothing function, like it
should, but is then allowing the text to be inserted into the Text
widget.

Here is the code:

self.framebody.tag_config("name", underline=1)
self.framebody.tag_bind("name", "<Any-KeyPress>", self.donothing)

line = '\t%s' % ('sometext')
self.framebody.insert(END, line, "name")

def donothing(self):
return 'break'

This donothing function should keep the tcl bind from inserting the
text...at least so I thought.

This bind does work on the text widget as a whole, but on a individual
tag, it does not.

Can anyone help me out with this?

Thanks in advance,
Shannon
Have you tried capturing the KeyRelease events as well?

regards
Steve
 
C

Christopher Subich

shannonl said:
Hi all,

For some reason this bind is calling the donothing function, like it
should, but is then allowing the text to be inserted into the Text
widget. [...]
This bind does work on the text widget as a whole, but on a individual
tag, it does not.

You're trying to prevent a user from editing the text -within- a single
tag. Does Tk even support this?

Is your bind-applied-to-tag even firing, when the user presses a key
within a tag?
 
S

shannonl

I don't know if Tk supports this or not. I guess it just made since to
me that it should. If you can bind events to a tag then why not a tag
inside a text widget. The actual bind itself works. donothing() is
called, the problem is that after donothing() is called and I return
"break", Tk continues to insert the text entered. I am just try to
lock down one particular part of a text line while leaving the rest of
the line, which has a different tag, alone.
 
J

jean-marc

but you don't want to use the state=DISABLED option because it gray's
out the field showing people that it is not available for editing,
right?
 
S

shannonl

Thanks for your help and the link. From the link it sounds like no one
is sure how this is supposed to act. I will dig around in the tcl
forum and see what I can find.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top