faqts : Computers : Programming : Languages : Python : Snippets : Tkinter

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

6 of 7 people (86%) answered Yes
Recently 5 of 6 people (83%) answered Yes

Entry

Key bindings in Tkinter

Jul 5th, 2000 09:59
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 64, Michael P. Reilly


"""
Packages: gui.tkinter
"""


"""
: I have a top level window, formed by Tk(), to which I have bound some
: keys (say 'f') to perform some action (like run 'fortune').  This
: contains an entry widget in which I want to type something.  The problem
: is that when I type a letter to which has been bound an action, that
: action gets fired off.  What do I need to do so that this doesn't
: happen?

: I have considered unbinding the events on entry.enter and binding them
: again on entry.leave, but that seems very ugly.

: If it has any relevance, I'm using Python 1.5.1 under Windows.

Without seeing the code, I can not say what is wrong.  But venturing a
guess, it sounds like you are using the bind_all method instead of
bind.  This doesn't sound like a platform specific problem.

When you bind an event to a top level window, then all windows mastered
from it will use that binding.  What you might want is a popup window:
"""

from Tkinter import *

master = Tk()  # create the root appl widget
master.bind('<Key-f>', lambda event: fortune())
master.bind_class('Entry', '<Return>', lambda event: root.destroy())
Label(master, text='Enter your age:').pack()
Entry(master).pack()  # affected by root.bind()

top = Toplevel(master)
Label(top, text='Enter your name:').pack()
Entry(top).pack()     # is not affected by the root.bind()

master.mainloop()