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

后端 未结 11 1102
渐次进展
渐次进展 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

    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: 
    

    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.

提交回复
热议问题