Jawad Malik
Jawad Malik

Reputation: 618

Insert value into Text Widget removing previous Value - Tkinter

I am using a backspace button on Text Widget and i want to remove the last character whenever user click on backspace button. I am using this code but it is not removing the previous value.

input = entry.get("1.0", 'end-2c')
        entry.insert("1.0",input, END)

here i am declaring Text Widget

entry = Text(Keyboard_App, width=97, height=8)

Upvotes: 2

Views: 1173

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

If you want to delete text, you must explicitly call the delete method. Calling insert alone will not overwrite existing text -- it inserts, as the name implies. delete takes one or two indexes. If just one index is given, it deletes the character immediately after the index. If two, it deletes all characters between the two indexes.

If you want to delete the entire contents of the widget, you can use the indexes "1.0" and "end":

entry.delete("1.0", "end")

To delete just the last character you can use just "end-2c", which means the end index minus two characters (minus one refers to just before the trailing newline, so minus two means the character before the trailing newline).

entry.delete("end-2c")

Upvotes: 3

Related Questions