无法清除输出文本:tkinter。Tcl错误:错误的文本索引"0"



当运行以下代码并单击" TKINTER"按钮时;它产生以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "D:Scypylibtkinter__init__.py", line 1699, in __call__
    return self.func(*args)
  File "D:modpuDocumentsPythonDelMe.py", line 5, in click
    OutputBox.delete(0, END)
  File "D:Scypylibtkinter__init__.py", line 3133, in delete
    self.tk.call(self._w, 'delete', index1, index2)
_tkinter.TclError: bad text index "0"

由于某种原因,代码在输入框中成功清除了文本,但未能在输出框中清除文本(它崩溃了)。

对此的任何帮助将不胜感激,谢谢。

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()

在这种情况下,这是解决方案:

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()
def click(): 
    OutputBox.delete('1.0', END)

有时系统尝试在闲置运行时不支持整数,我知道它令人沮丧,但是是的。

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

在python 3.8.1中为" 1.0"扔错误。

相关内容

最新更新