python - 无法清除输出文本 : tkinter. TclError:错误的文本索引 "0"

标签 python user-interface tkinter textbox

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

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"

由于某些原因,代码成功清除了输入框中的文本,但无法清除输出框中的文本(而是崩溃)。

如有任何帮助,我们将不胜感激,谢谢。

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

关于python - 无法清除输出文本 : tkinter. TclError:错误的文本索引 "0",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47502212/

相关文章:

c++ - 需要支持 OpenGL 的跨平台 GUI 工具包

android - 如何从异步任务中获取对当前窗口的引用?安卓

user-interface - 在JUCE中将GUI编辑器用于音频插件

python - 我的Python计算器程序允许输入字符

python - 如何向 Tkinter 添加部分透明的图像?

python - PyCharm 不建议自动完成并标记未解析的引用

python - 使用 argparse.ArgumentParser() 获取分组结构

python - 删除 Pandas 数据框中多次出现的重复值

python - 在 MacBook Air Catalina 中,conda 上的 pycafe 安装失败

python - 如何跟踪单击了哪个文本条目?