[英]Cannot clear output text: tkinter.TclError: bad text index "0"
When the following code is ran and the tkinter button is clicked;当运行以下代码并单击 tkinter 按钮时; 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()
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()
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.有时,当您尝试在 IDLE 中运行时系统不支持整数,我知道这令人沮丧,但是这确实有效。
def click():
OutputBox.delete('0', END)
Worked for me in Python 3.8.1 as '1.0' was throwing errors.在 Python 3.8.1 中为我工作,因为“1.0”抛出错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.