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

Discussion in 'Python' started by shannonl, Oct 25, 2005.

  1. shannonl

    shannonl Guest

    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
    shannonl, Oct 25, 2005
    #1
    1. Advertising

  2. shannonl

    Guest

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

    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-----
    , Oct 25, 2005
    #2
    1. Advertising

  3. shannonl

    Steve Holden Guest

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

    shannonl wrote:
    > 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
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC www.holdenweb.com
    PyCon TX 2006 www.python.org/pycon/
    Steve Holden, Oct 26, 2005
    #3
  4. Re: textwidget.tag_bind("name", "<Any-KeyPress>", self.donothing)not working

    shannonl wrote:
    > 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?
    Christopher Subich, Oct 26, 2005
    #4
  5. shannonl

    shannonl Guest

    No I haven't, but I will give it a try.

    Thanks for your reply!
    shannonl, Oct 26, 2005
    #5
  6. shannonl

    shannonl Guest

    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.
    shannonl, Oct 26, 2005
    #6
  7. shannonl

    jean-marc Guest

    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?
    jean-marc, Oct 26, 2005
    #7
  8. shannonl

    jean-marc Guest

    Sorry, kinda wrote over your intentions...
    jean-marc, Oct 26, 2005
    #8
  9. shannonl

    jean-marc Guest

    jean-marc, Oct 26, 2005
    #9
  10. shannonl

    shannonl Guest

    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.
    shannonl, Oct 31, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    578
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    592
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    398
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    371
    falcon
    Jul 31, 2005
  5. Bart Kastermans
    Replies:
    6
    Views:
    398
    Bart Kastermans
    Jul 13, 2008
Loading...

Share This Page