Tkinter+ backspace characters (and an re.sub problem)

Yitz bmcyitz at
Wed Dec 19 16:42:31 CET 2001

"David Mallwitz" <dmallwitz at> wrote in message news:<3c202041$1_6 at>...
>The Tkinter Text widget doesn't understand... '\x08' as the
> backspace character...
> a solution... doing a
> regexp sub on the string before sending it to the text widget. That seemed
> like a good idea...

I agree.

> >>>string = 'all work and no play makes Jack\x08\x08\x08\x08Dave a dull boy'
> >>>a = re.compile('.\x08')

Watch out - the "." will also match \x08.

> >>> a.sub('', string)   ###fails - would have thought there should be 3
> '\x08's left, or none at all

You need to say string = a.sub('', string). You can't get all of the
backspaces at once; you will need a loop. Here is one way:

a = re.compile('[^\x08]\x08')n = 1
while n:
  (string, n) = a.subn('', string)
string = re.sub('^\x08+', '', string)
Hope this helps,

More information about the Python-list mailing list