views:

212

answers:

4

I'm trying to clear the entry widget after the user presses a button using tkinter in python. I tried using ent.delete(0,END) but I got an error saying that strings dont have the attribute delete

here is my code: 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()
A: 

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
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
+2  A: 

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. You can probably shorten it; I was following a model in the tutorial.

import sys
import Tkinter

class App:
    def __init__(self, master):
        fr = Tkinter.Frame(master, height=42, width=42)
        fr.pack()
        self.txt_ent = Tkinter.Entry(fr)
        self.txt_ent.pack()
        self.clear_button = Tkinter.Button(fr, text="clear text", command=self.clear_text)
        self.clear_button.pack()  

    def clear_text(self):
        self.perform_random_action()
        self.txt_ent.delete(0, Tkinter.END)

    def perform_random_action():
        # Do random stuff

def main():
    main_win = Tkinter.Tk()
    app = App(main_win)
    main_win.mainloop()
    return 0

if __name__ == "__main__":
    sys.exit(main())
GreenMatt
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
@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
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
@Dan: I've added some code to get you started on the random action; please see the random module at http://www.python.org if you haven't already. If you need more help with the random stuff, I suggest you search for other questions that might help, and if you don't find any, ask a separate question. <soapbox> Also, I don't want to seem obnoxious, but if you find answers helpful, you may want to up vote them and even accept the one which is most helpful - it will encourage more folks to help and also help out others who come after you who are looking for help on the same topic. </soapbox>
GreenMatt
+1  A: 

It seems you are calling .delete on a string. Assuming the entry_widget variable holds the widget itself, you probably did this somewhere:

ent = entry_widget.get()

so now ent is just a string (which has no idea where it came from and no way to affect the widget). Instead, do it on the widget itself:

entry_widget.delete(0, END)

If this doesn't help, please post your code.

Beni Cherniavsky-Paskin
A: 

ent.delete(0,END) is the correct answer. If you get the error you say you do, you have an error in your code. You will need to show us the code if you are unable to spot the error yourself.

Bryan Oakley