Problem deleting text in TK widget

John Grayson johngrayson at home.com
Thu Jul 27 16:14:04 EDT 2000


In article <8lpvj7$v1h$1 at nnrp1.deja.com>,
  dogboy777 at my-deja.com wrote:
> I'm trying to delete text in a TK Text field, and I'm getting an error
I
> don't understand.  The code is:
>
> 		self.logarea = Text(master, height=35, width=30)
> 		self.logarea.insert(END, 'This is a test')
> 		self.logarea.delete(1,3)
>
> The resulting error is:
>
> Traceback (most recent call last):
>   File "junkyard.py", line 109, in ?
>     junk = Junkyard(root)
>   File "junkyard.py", line 20, in __init__
>     self.createMainScreen()
>   File "junkyard.py", line 34, in createMainScreen
>     self.logarea.delete(1,3)
>   File "C:\PYTHON16\lib\lib-tk\Tkinter.py", line 1643, in delete
>     self.tk.call(self._w, 'delete', index1, index2)
> TclError
>
> Any idea why Tcl is unhappy?

Sure...

The Text widget is a multiline widget, so the index must be
given as line.column...

from Tkinter import *

class App(Tk):

  def __init__(self):
    Tk.__init__(self)
    self.logarea = Text(self, height=35, width=30)
    self.logarea.pack()
    self.logarea.insert(END, 'This is a test')
    self.logarea.delete(1.1,1.3)

App().mainloop()


Note: just to confuse matters 'line' is 1-based wheras
'column' is 0-based.

   John Grayson


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list