Home > Mobile >  tkinter - how to stop user typing when shortcut key is pressed
tkinter - how to stop user typing when shortcut key is pressed

Time:12-10

I've created a textbox with tkinter. The following code stops a newline being created when Ctrl Return is pressed.

textbox = Text()
textbox.pack()
textbox.bind("<Control-Return>", lambda event: "break")

However, I also want to call a function when the user types Ctrl Return. The following calls the function, but then creates a newline:

textbox = Text()
textbox.pack()
textbox.bind("<Control-Return>", lambda event: "break")
textbox.bind("<Control-Return>", lambda event: my_func())

If I swap the third and fourth line, there is no newline, but the function isn't called either.

Is there a way of both preventing a newline and calling the function?

CodePudding user response:

Create a proper function, then it's easy to do whatever you want.

def handle_control_return(event):
    my_func()
    return "break"

textbox.bind("<Control-Return>", handle_control_return)
  • Related