M.P.Tree
M.P.Tree

Reputation: 131

Cannot clear output text: tkinter.TclError: bad text index "0"

When the following code is ran and the tkinter button is clicked; it produces the following error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "D:\Scypy\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "D:\modpu\Documents\Python\DelMe.py", line 5, in click
    OutputBox.delete(0, END)
  File "D:\Scypy\lib\tkinter\__init__.py", line 3133, in delete
    self.tk.call(self._w, 'delete', index1, index2)
_tkinter.TclError: bad text index "0"

For some reason the code successfully clears the text in the input box but fails to clear the text in the output box (it crashes instead).

Any help for this would be appreciated, thank you.

from tkinter import *

def click():
    MainTextBox.delete(0, END)  #This works
    OutputBox.delete(0, END) #This doesn't work

GUI = Tk()
MainTextBox = Entry(GUI, width = 20, bg = "white")
MainTextBox.grid(row = 0, column = 0, sticky = W)
Button(GUI, text = "SUBMIT", width = 6, command = click).grid(row = 1, column = 0, sticky = W)
OutputBox = Text(GUI, width = 100, height = 10, wrap = WORD, background = "orange")
OutputBox.grid(row = 4, column = 0, sticky = W)
OutputBox.insert(END, "Example text")

GUI.mainloop()

Upvotes: 9

Views: 26362

Answers (3)

Guy Cherkesky
Guy Cherkesky

Reputation: 21

def click(): 
    OutputBox.delete('0', END)

Worked for me in Python 3.8.1 as '1.0' was throwing errors.

Upvotes: 2

Tarun Singh
Tarun Singh

Reputation: 21

def click(): 
    OutputBox.delete('1.0', END)

Sometimes system doesn't support integer when you try it to run in IDLE, I know its frustrating but yeah this sure works.

Upvotes: 2

Josh Dinsdale
Josh Dinsdale

Reputation: 375

In this case, this is the solution:

from tkinter import *

def click():
    MainTextBox.delete(0, END)  
    OutputBox.delete('1.0', END) 

GUI = Tk()
MainTextBox = Entry(GUI, width = 20, bg = "white")
MainTextBox.grid(row = 0, column = 0, sticky = W)
Button(GUI, text = "SUBMIT", width = 6, command = click).grid(row = 1, column = 0, sticky = W)
OutputBox = Text(GUI, width = 100, height = 10, wrap = WORD, background = "orange")
OutputBox.grid(row = 4, column = 0, sticky = W)
OutputBox.insert(END, "Example text")

GUI.mainloop()

Upvotes: 12

Related Questions