December 20, 2001 | Fredrik Lundh
Q. The Tkinter Text widget doesn’t understand ‘\b’ or ‘\x08’ as the backspace character when sent a string containing either of them. /…/ Any ideas on how to tell the widget to process a backspace? Or on how to construct an RE that will match multiple ‘\b’s?
Something like this should work:
from Tkinter import * import re message = 'all work and no play makes Jack\x08\x08\x08\x08Dave a dull boy' text = Text() text.pack() # find backspace/non-backspace runs for fragment in re.findall("\x08+|[^\x08]+", message): if fragment == "\x08": # delete len(fragment) characters before current insertion point text.delete("%s-%dc" % (INSERT, len(fragment)), INSERT) else: text.insert(INSERT, fragment) mainloop()