level42 level42 -4 years ago 139
Python Question

Having some trouble clearing a textbox - tkinter - Python

I've read a few threads all over the internet regarding clearing a text box on tkinter. Basically everyone says it's simple:

text.delete("1.0", END)


However, perhaps it has something to do with the way I structured it, or the way I'm calling it, but for some reason, this does not work for me. It simply does nothing.

I've tried re-positioning the
def
, and re-writing the
text.delete("1.0", END)
in a number of ways, most of which lead me to other errors, but I cannot seem to get this to work.

Ultimately, what I'm trying to accomplish is that when I click a button, the text box will clear, before populating with new information.

Below is my code.

from tkinter import *
from PIL import Image, ImageTk
import functions

class MainWindow(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()

def initUI(self):
self.parent.title("pyTicket")

# TOOLBAR ####################################################
toolbar = Frame(self.parent, bd=1, relief=RAISED)

self.img = Image.open("Icons\startupcheck.png")
eimg = ImageTk.PhotoImage(self.img)
startupButton = Button(toolbar, text="Re-Check ", image=eimg, compound="left", relief=RAISED, command=self.StartUpChecker)
startupButton.image = eimg
startupButton.pack(side=RIGHT, padx=2, pady=2)

toolbar.pack(side=TOP, fill=X)

self.parent.config(menu=menubar)
self.pack(anchor=N, side=TOP, fill=X, expand=False)
# TOOLBAR ####################################################

# TEXTBOX ####################################################
self.textbox = Text(self, wrap="word", height=5)
self.textbox.pack(side="bottom", fill="both", expand=True)
self.textbox.tag_configure("TextBox", foreground="#b22222")
self.pack(anchor=S, side=BOTTOM, fill=BOTH, expand=True)
# TEXTBOX ####################################################

# Functions ###################################################
def StartUpChecker(self):
self.clear_text()
functions.StartUpChecker()

def clear_text(self):
self.textbox.delete("1.0", END)

class TextRedirector(object):
def __init__(self, widget, tag="stdout"):
self.widget = widget
self.tag = tag

def write(self, str):
self.widget.configure(state="normal")
self.widget.insert("end", str, (self.tag,))
self.widget.configure(state="disabled")
def main():
root = Tk()
#Width X Height
root.geometry("500x300+300+300")
root.update()
root.minsize(400,200)
app = MainWindow(root)
root.mainloop()

if __name__ == '__main__':
main()

Answer Source

You don't appear to actually use the TextRedirector class in the code you posted, but if you're using it in your actual code, note that its .write() method leaves the textbox in a disabled state - which prevents ALL modifications, even those resulting from code instead of direct user action. Your .clear_text() method needs to temporarily enable the textbox so that you can modify it, exactly as .write() does.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download