Tkinter default bindings

Discussion in 'Python' started by Phil Schmidt, Mar 1, 2004.

  1. Phil Schmidt

    Phil Schmidt Guest

    I have an Entry widget inside a Frame. The Frame contains other
    widgets as well. I have bound the <Key> event to the Frame, but I
    don't want the Frame to receive the event when the Entry widget has
    focus.

    So, I bound the <Key> event to the Entry widget (bound to method
    a_key()). This works, except that both the Entry and Frame widgets get
    the event. This also forces me to implement event handlers, which is
    fine for the Frame since that's what I want to do. But I want the
    Entry to use its default handler, not my own handler. Rather than
    duplicate all that functionality, I found I could accomplish this by
    temporarily unbinding the events, re-generating the event, and then
    re-binding the events, as follows:

    def a_key(self, e):
    self.top.unbind('<Key>')
    self.entry.unbind('<Key>')
    self.entry.event_generate('<Key>',
    keycode=e.keycode,
    keysym=e.keysym,
    )
    self.entry.bind('<Key>', self.a_key)
    self.top.bind('<Key>', self._parent_class__keypress)
    return 'break'

    This works, but it's kludgy. Is there a better way to do this? I just
    want the Entry widget to receive the event when it has focus, behave
    in its default manner, and not have the event propagate upward to
    containing widgets.
     
    Phil Schmidt, Mar 1, 2004
    #1
    1. Advertising

  2. Phil Schmidt wrote:
    > I have an Entry widget inside a Frame. The Frame contains other
    > widgets as well. I have bound the <Key> event to the Frame, but I
    > don't want the Frame to receive the event when the Entry widget has
    > focus.
    >
    > So, I bound the <Key> event to the Entry widget (bound to method
    > a_key()). This works, except that both the Entry and Frame widgets get
    > the event. This also forces me to implement event handlers, which is
    > fine for the Frame since that's what I want to do. But I want the
    > Entry to use its default handler, not my own handler. Rather than
    > duplicate all that functionality, I found I could accomplish this by
    > temporarily unbinding the events, re-generating the event, and then
    > re-binding the events, as follows:
    >
    > def a_key(self, e):
    > self.top.unbind('<Key>')
    > self.entry.unbind('<Key>')
    > self.entry.event_generate('<Key>',
    > keycode=e.keycode,
    > keysym=e.keysym,
    > )
    > self.entry.bind('<Key>', self.a_key)
    > self.top.bind('<Key>', self._parent_class__keypress)
    > return 'break'
    >
    > This works, but it's kludgy. Is there a better way to do this? I just
    > want the Entry widget to receive the event when it has focus, behave
    > in its default manner, and not have the event propagate upward to
    > containing widgets.


    You could just find out who has focus (root.focus_get() and compare with
    the entry... something like (untested)

    def a_key(self, event):
    has_focus = root.focus_get()
    if theEntry==has_focus:
    print "the entry binding"
    return "break"
    else:
    print "the frame binding"


    Martin
     
    Martin Franklin, Mar 2, 2004
    #2
    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. Will Woods

    Tkinter and readline key bindings

    Will Woods, Sep 2, 2003, in forum: Python
    Replies:
    0
    Views:
    385
    Will Woods
    Sep 2, 2003
  2. p.kosina

    bindings in Tkinter

    p.kosina, Jun 14, 2004, in forum: Python
    Replies:
    3
    Views:
    475
    Jeff Epler
    Jun 16, 2004
  3. Frans Englich
    Replies:
    0
    Views:
    322
    Frans Englich
    Mar 8, 2005
  4. Replies:
    0
    Views:
    702
  5. Roger
    Replies:
    7
    Views:
    717
    Roger
    Jan 13, 2009
Loading...

Share This Page