Search code examples
pythontkintertext-widget

Deleting a whole word in tkinter text widget


Is there any way to delete a whole word in tkinter?

Here's the code:

from tkinter import *

root = Tk()

def delete_whole_word(event):
    # Code to delete a whole word
    pass

text = Text(root, width = 65 , height = 20 , font = "consolas 14")
text.grid(row = 0 , column = 0)

text.insert(1.0 , "This is a text widget")
text.mark_set(INSERT , END)

text.bind("<BackSpace>" , delete_whole_word)

mainloop()

Here when I press the BackSpace key, I want to delete the whole word instead of just deleting one character.

By deleting a whole word, I mean this:

enter image description here

Is there any way to achieve this in tkinter?

It would be great if anyone could help me out.


Solution

  • Use:

    def delete_whole_word(event):
        text.delete("insert-1c wordstart", "insert")
        return "break"