55

I'm trying to clear the Entry widget after the user presses a button using Tkinter.

I tried using ent.delete(0, END), but I got an error saying that strings don't have the attribute delete.

Here is my code, where I'm getting error on real.delete(0, END):

secret = randrange(1,100)
print(secret)
def res(real, secret):
    if secret==eval(real):
        showinfo(message='that is right!')
    real.delete(0, END)

def guess():
    ge = Tk()
    ge.title('guessing game')

    Label(ge, text="what is your guess:").pack(side=TOP)

    ent = Entry(ge)
    ent.pack(side=TOP)

    btn=Button(ge, text="Enter", command=lambda: res(ent.get(),secret))
    btn.pack(side=LEFT)

    ge.mainloop()
nbro
  • 15,395
  • 32
  • 113
  • 196
Dan
  • 8,263
  • 16
  • 51
  • 53

13 Answers13

96

After poking around a bit through the Introduction to Tkinter, I came up with the code below, which doesn't do anything except display a text field and clear it when the "Clear text" button is pushed:

import tkinter as tk

class App(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master, height=42, width=42)
        self.entry = tk.Entry(self)
        self.entry.focus()
        self.entry.pack()
        self.clear_button = tk.Button(self, text="Clear text", command=self.clear_text)
        self.clear_button.pack()

    def clear_text(self):
        self.entry.delete(0, 'end')

def main():
    root = tk.Tk()
    App(root).pack(expand=True, fill='both')
    root.mainloop()

if __name__ == "__main__":
    main()
nbro
  • 15,395
  • 32
  • 113
  • 196
GreenMatt
  • 18,244
  • 7
  • 53
  • 79
  • 3
    You can supply the argument END (or "end") instead of computing the length of the data. Since you say it didn't work but don't define "didn't work" (ie: did you get an error, or did it silently fail?), my guess is you used an unqualified "END". Try "Tkinter.END" instead. When I use that in the above code it works just fine. – Bryan Oakley Feb 14 '10 at 18:05
  • @Bryan: Ah, I just used END, not Tkinter.END (the tutorial used `from ... import` instead of just import). Thanks! The fix is in the code. – GreenMatt Feb 14 '10 at 19:10
  • I want the button to perform 2 actions at the same time. the first is to perform a random action and the 2nd is to clear the entry – Dan Feb 15 '10 at 05:43
  • Note that if you do this, the state of your Entry widget must be normal. Have personally run into this error :). – Chris Fowl Apr 08 '19 at 16:05
  • 1
    If you are using Python 3.x version, you have to more careful about to clear the entry box - e.delete(0, tkinter.END) – Ravi K Jan 24 '20 at 15:13
18

I'm unclear about your question. From http://effbot.org/tkinterbook/entry.htm#patterns, it seems you just need to do an assignment after you called the delete. To add entry text to the widget, use the insert method. To replace the current text, you can call delete before you insert the new text.

e = Entry(master)
e.pack()

e.delete(0, END)
e.insert(0, "")

Could you post a bit more code?

Charles Merriam
  • 19,908
  • 6
  • 73
  • 83
  • 1
    The answer "it seems you just need to do an assignment after you called the delete" in no way answers the question "how to clear the entry widget". – Bryan Oakley Feb 14 '10 at 18:10
5

real gets the value ent.get() which is just a string. It has no idea where it came from, and no way to affect the widget.

Instead of real.delete(), call .delete() on the entry widget itself:

def res(ent, real, secret):
    if secret == eval(real):
        showinfo(message='that is right!')
    ent.delete(0, END)

def guess():
    ...
    btn = Button(ge, text="Enter", command=lambda: res(ent, ent.get(), secret))
nbro
  • 15,395
  • 32
  • 113
  • 196
Beni Cherniavsky-Paskin
  • 9,483
  • 2
  • 50
  • 58
4

If in case you are using Python 3.x, you have to use

txt_entry = Entry(root)

txt_entry.pack()

txt_entry.delete(0, tkinter.END)

Ravi K
  • 215
  • 1
  • 3
  • 13
  • I'm passing by to remind everyone that if you ```import tkinter as tk```, then you would have to use ```txt_entry.delete(0, tk.END)``` – NoahVerner Jan 17 '22 at 00:47
1

You shall proceed with ent.delete(0,"end") instead of using 'END', use 'end' inside quotation.

 secret = randrange(1,100)
print(secret)
def res(real, secret):
    if secret==eval(real):
        showinfo(message='that is right!')
    real.delete(0, END)

def guess():
    ge = Tk()
    ge.title('guessing game')

    Label(ge, text="what is your guess:").pack(side=TOP)

    ent = Entry(ge)
    ent.pack(side=TOP)

    btn=Button(ge, text="Enter", command=lambda: res(ent.get(),secret))
    btn.pack(side=LEFT)

    ge.mainloop()

This shall solve your problem

0

First of all, make sure the Text is enabled, then delete your tags, and then the content.

myText.config(state=NORMAL)
myText.tag_delete ("myTags")
myText.delete(1.0, END)

When the Text is "DISABLE", the delete does not work because the Text field is in read-only mode.

Creos
  • 2,445
  • 3
  • 27
  • 45
0
def clear():                                                                           
        global input                                                                    
        abc =  
        input.set(abc)                                                                     

root = Tk()                                                               
input = StringVar()                                                             
ent = Entry(root,textvariable =                                       input,font=('ariel',23,'bold'),bg='powder                            blue',bd=30,justify='right').grid(columnspan=4,ipady=20)                       
Clear = Button(root,text="Clear",command=clear).pack()                       

Input is set the textvariable in the entry, which is the string variable and when I set the text of the string variable as "" this clears the text in the entry

Ira Watt
  • 2,095
  • 2
  • 15
  • 24
0

Simply define a function and set the value of your Combobox to empty/null or whatever you want. Try the following.

def Reset():
    cmb.set("")

here, cmb is a variable in which you have assigned the Combobox. Now call that function in a button such as,

btn2 = ttk.Button(root, text="Reset",command=Reset)
MD Sulaiman
  • 185
  • 1
  • 15
0

if you add the print code to check the type of real, you will see that real is a string, not an Entry so there is no delete attribute.

def res(real, secret):
    print(type(real))
    if secret==eval(real):
        showinfo(message='that is right!')
    real.delete(0, END)

>> output: <class 'str'>

Solution:

secret = randrange(1,100)
print(secret)

def res(real, secret):
    if secret==eval(real):
        showinfo(message='that is right!')
    ent.delete(0, END)    # we call the entry an delete its content

def guess():

    ge = Tk()
    ge.title('guessing game')

    Label(ge, text="what is your guess:").pack(side=TOP)

    global ent    # Globalize ent to use it in other function
    ent = Entry(ge)
    ent.pack(side=TOP)

    btn=Button(ge, text="Enter", command=lambda: res(ent.get(),secret))
    btn.pack(side=LEFT)

    ge.mainloop()

It should work.

Luc Thi
  • 21
  • 1
  • 1
0

From my experience, Entry.delete(0, END) sometimes didn't work when the state of entry widget is DISABLED. Check the state of Entry when Entry.delete(0, END), doesn't work and if the value of entry widget remains, call entry.update() to reflect the result of delete(0, END).

ouflak
  • 2,458
  • 10
  • 44
  • 49
0

You can Use Entry.delete(1.0, END) it deletes every thing in the entry field, if u use only Entry.delete(1.0)it deletes the last word you inputed ->Example text to ->xample text

P4kaaa
  • 11
  • 1
-2

if none of the above is working you can use this->

idAssignedToEntryWidget.delete(first = 0, last = UpperLimitAssignedToEntryWidget)

for e.g. ->

id assigned is = en then

en.delete(first =0, last =100)

-8

Try with this:

import os
os.system('clear')
nbro
  • 15,395
  • 32
  • 113
  • 196