Home > Mobile >  Tkinter - bind crtl enter to make a new line in textbox (Text)
Tkinter - bind crtl enter to make a new line in textbox (Text)

Time:03-05

I already made that when somebody presses Enter the text in the textbox will be deleted and data will be transferred to another method, however, I want to bind also ctrl Enter to make a new line in the textbox, like Zoom's chat and some other platforms' chat, however, I don't know how to do so over Tkinter, and how to take the data, and replace the new line with "\n" that the Enter created in textbox since it seems that the new line in the textbox isn't actually "\n" whenever I take it as a string.

CodePudding user response:

To bind control-Enter to insert a literal newline and to not do the default action, you could do it like in the following example. Note that the event is <Control-Return>, and the function returns the string "break" to prevent other handlers from handling the event.


def insert_newline(event):
    event.widget.insert("insert", "\n")
    return "break"

text = tk.Text(...)
text.bind("<Control-Return>", insert_newline)
  • Related