0

I am binding a normal key do run a function using Tkinter, like this:

from tkinter import *
root = Tk()
T = Text()
T.pack()
root.bind_all('h', lambda event: print('HI'))

When you press the h key, it prints HI, but also it types an h into the text box. Is there any easy way to block the h key from doing its normal purpose?

nedla2004
  • 1,115
  • 3
  • 14
  • 29
  • You can add a whole bunch of validation for that text field, or you can change the keybind to something like Ctrl-H like everyone else does. – TigerhawkT3 Nov 22 '16 at 01:33

1 Answers1

2

To inhibit the default behaviour, return the string "break"

def h_key(event):
    ...
    return "break"

root.bind_all('h', h_key)

Also, in your case you don't need to use lambda. In general, you should avoid using lambda unless it really is the best tool for the job. Most often it's best to write a function for your bindings. Functions are much easier to debug and to maintain over time.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685