How to clear the Entry widget after a button is pressed in Tkinter?

后端 未结 11 1074
渐次进展
渐次进展 2020-12-01 10:44

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

相关标签:
11条回答
  • 2020-12-01 10:47

    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)
    
    0 讨论(0)
  • 2020-12-01 10:47

    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.

    0 讨论(0)
  • 2020-12-01 10:51

    Try with this:

    import os
    os.system('clear')
    
    0 讨论(0)
  • 2020-12-01 10:54

    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)

    0 讨论(0)
  • 2020-12-01 11:00

    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?

    0 讨论(0)
  • 2020-12-01 11:02
    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

    0 讨论(0)
提交回复
热议问题