How do I delete a text content when I press a botton using tkinter?

399 Views Asked by At

I am creating a code that adds numbers or words to a text box and then switches the numbers or words around. I want to create a clear button that deletes all the content in the textbox and when the user presses one of the other buttons it appears again. When I created the code the clear button does not work, how can I fix it?

from tkinter import *

 

class main:

    def __init__(self, test):

        self.lbl1=Label(text="Select Button")

        self.btn6 = Button(test, text="Random Number 1", command=lambda : self.Label(float(3+3)))

        self.Example = Text(window, height = 3, width = 10)

        self.b1=Button( test, text='Random Number 2', command= lambda: self.testingLabel(float(2+3)))

        self.clear = Button(test,text="Clear", command= self.clear_text)

      

        

        v0=IntVar()

        v0.set(1)

        self.s1=Radiobutton(test, text="Switch", variable=v0,value=1)

        self.s2=Radiobutton(test, text="Switch Back", variable=v0,value=2)

       

        self.lbl1.place(x=0, y=0)

        self.btn6.place(x=0, y=50)

        self.b1.place(x=0 , y= 100 )

        self.s1.place(x=0,y=180)

        self.s2.place(x=80, y=180)

        self.clear.place(x = 200 , y = 80 )

       

    def testingLabel(self,text):

        self.Example.insert(1.0, text)

        self.Example.pack()

 

    def Label(self,text):

        self.Example.insert(1.0, text)

        self.Example.pack()

    def clear_text(self):

       

        self.Example.delete("0",END)

        self.Example.pack()

 

window=Tk()

mywin=main(window)

window.title("Testing")

window.geometry("400x300+10+10")

window.mainloop()
1

There are 1 best solutions below

3
On

Change your code to read this.

def clear_text(self):

    self.Example.delete("1.0" ,END) # changed your "0" to "1.0" for clearing textbox

    self.Example.pack()