删除tkinter中的条目小部件

时间:2018-05-05 01:25:18

标签: python tkinter

尝试使用按钮删除条目小部件,但不断收到错误

AttributeError: 'object' object has no attribute 'delete'

我将代码缩短为:

from tkinter import *
e = object()

def getname():
    global name
    e = Entry(root, textvariable=name)
    e.pack()
    e.focus_set()
    b = Button(root, text = "Get", width = 10, command = callback)
    b.pack()
    b2 = Button(root, text = "Delete", width = 10, command = delete_entry)
    b2.pack()

def callback():
    username = name.get()
    print(username)

def delete_entry():
    e.delete(0, "end")

root = Tk()
name = StringVar()
getname()
root.mainloop()

感谢您的时间

1 个答案:

答案 0 :(得分:0)

您可以使用此代码(完全删除条目框):

def delete_entry():
    e.destroy()

或者您只想删除Entry小部件中的文本?告诉我,我会更新我的答案。