[Tutor] problem clearing a text widget

Chris Roy-Smith chris_roysmith at internode.on.net
Thu Sep 5 02:45:11 EDT 2019


Hi
setup: python 3.6.8, OS Linux (ubuntu 18.04)

I have been trying to clear a text widget using the delete method, but I 
only get the traceback below, when I click on delete button. I get this 
message irrespective of how much text I have put into it. I'm missing 
something, but I can't figure out what. Please can somebody point me in 
the right direction?

regards, Chris Roy-Smith

==============================================================
chris at chris-X451MA:~/Scripts/python3/experiments$ ./text.py
Exception in Tkinter callback
Traceback (most recent call last):
   File "/usr/lib/python3.6/tkinter/__init__.py", line 1705, in __call__
     return self.func(*args)
   File "./text.py", line 11, in DeleteText
     tfield.delete(0,END)
   File "/usr/lib/python3.6/tkinter/__init__.py", line 3139, in delete
     self.tk.call(self._w, 'delete', index1, index2)
_tkinter.TclError: bad text index "0"

=================================================================

code

#! /usr/bin/python3
'''
demo for text widget delete method
'''
from tkinter import *

def InsertText():
     tfield.insert(END,"Demo only\n")

def DeleteText():
     tfield.delete(0,END)

master=Tk()

tfield = Text(master, height=15, width=60)
S=Scrollbar(master)
tfield.configure(yscrollcommand=S.set)
S.configure(command=tfield.yview)
S.grid(row=0, column=2)
tfield.grid(row=0, column=0, columnspan=2)
S.config(command=tfield.yview)
tfield.config(yscrollcommand=S.set)

Button(master, text="Insert", command=InsertText).grid(row=5, column=0)
Button(master, text="Delete", command=DeleteText).grid(row=5, column=1)
Button(master, text="Exit", command=master.destroy).grid(row=5, column=2)

master.mainloop()


More information about the Tutor mailing list