Python 提交新条目时删除Tkinter中的标签

Python 提交新条目时删除Tkinter中的标签,python,tkinter,Python,Tkinter,我试图通过Tkinter编写一个函数,结果如下: 用户在某些输入中键入并提交 结果,按钮下方会出现一个结果标签 当用户提交条目时,在条目中添加新输入时,先前的标签将被销毁,并且只显示新的结果标签 我已经搜索了多个答案,但是我没有成功地达到我的目标,因为下面的代码是当前代码。如果用户提交了多个输入,则会出现多个标签,单击“重置”按钮时,只会删除最后一个标签,而之前的标签仍会出现 以下是我尝试过的: root = Tk() root.title("Search") root.ge

我试图通过Tkinter编写一个函数,结果如下: 用户在某些输入中键入并提交 结果,按钮下方会出现一个结果标签 当用户提交条目时,在条目中添加新输入时,先前的标签将被销毁,并且只显示新的结果标签

我已经搜索了多个答案,但是我没有成功地达到我的目标,因为下面的代码是当前代码。如果用户提交了多个输入,则会出现多个标签,单击“重置”按钮时,只会删除最后一个标签,而之前的标签仍会出现

以下是我尝试过的:

root = Tk()
root.title("Search")
root.geometry("400x400")
 
greeting= Label(root, text="Hi, Please insert input" )
greeting.pack()
 
search=[]
 
e = Entry(root, width=50)
e.pack(padx=50)
 
def myClick(event=None):
    global mySubmit
    global reply
    mySubmit=Label(root, text=results())
    search.append(e.get())
    print(search)
    e.delete(0,"end")
    mySubmit.pack()
 
def results():
    global result
    if e.get()=="":
        result = "Please Insert inquiry"
        return result
    else:
        result = "www.reply.com"
        return result
 
def reset():
    global mySubmit
    mySubmit.destroy()
    mySubmit = Label()
 
root.bind('<Return>', myClick)
 
myButton= Button(root, text="Submit", command=myClick)
myButton.pack()
bt2=Button(root,text='RESET',bg='lightblue',command=reset)
bt2.pack()
 
root.mainloop()
root=Tk()
root.title(“搜索”)
根几何(“400x400”)
问候语=标签(root,text=“嗨,请插入输入”)
greeting.pack()
搜索=[]
e=入口(根,宽度=50)
e、 包装(padx=50)
def myClick(事件=无):
全球mySubmit
全球答复
mySubmit=Label(root,text=results())
search.append(例如get())
打印(搜索)
e、 删除(0,“结束”)
mySubmit.pack()
def results():
全球结果
如果e.get()==“”:
结果=“请插入查询”
返回结果
其他:
result=“www.reply.com”
返回结果
def reset():
全球mySubmit
mySubmit.destroy()
mySubmit=Label()
root.bind(“”,myClick)
myButton=Button(root,text=“Submit”,command=myClick)
myButton.pack()
bt2=按钮(root,text='RESET',bg='lightblue',command=RESET)
bt2.pack()
root.mainloop()
使用
winfo_ismapped()
了解小部件是否已创建。只有当它不在时才打包

from tkinter import *

root = Tk()
root.title("Search")
root.geometry("400x400")
 
greeting= Label(root, text="Hi, Please insert input" )
greeting.pack()
 
search=[]
 
e = Entry(root, width=50)
e.pack(padx=50)

 
def myClick(event=None):
    global mySubmit
    global reply
    
    search.append(e.get())
    print(search)
    e.delete(0,"end")
    if(not mySubmit.winfo_ismapped()): #make a widget only if the mySubmit label is not mapped (created) till now
        mySubmit.pack()
 
def results():
    global result
    if e.get()=="":
        result = "Please Insert inquiry"
        return result
    else:
        result = "www.reply.com"
        return result

mySubmit=Label(root, text=results())    #declare it here outside the function scope

def reset():
    global mySubmit
    mySubmit.destroy()
    mySubmit = Label()
 
root.bind('<Return>', myClick)
 
myButton= Button(root, text="Submit", command=myClick)
myButton.pack()
bt2=Button(root,text='RESET',bg='lightblue',command=reset)
bt2.pack()
 
root.mainloop()
从tkinter导入*
root=Tk()
root.title(“搜索”)
根几何(“400x400”)
问候语=标签(root,text=“嗨,请插入输入”)
greeting.pack()
搜索=[]
e=入口(根,宽度=50)
e、 包装(padx=50)
def myClick(事件=无):
全球mySubmit
全球答复
search.append(例如get())
打印(搜索)
e、 删除(0,“结束”)
if(非mySubmit.winfo_ismapped()):#仅当mySubmit标签尚未映射(创建)时才制作小部件
mySubmit.pack()
def results():
全球结果
如果e.get()==“”:
结果=“请插入查询”
返回结果
其他:
result=“www.reply.com”
返回结果
mySubmit=Label(root,text=results())#在函数范围之外的此处声明它
def reset():
全球mySubmit
mySubmit.destroy()
mySubmit=Label()
root.bind(“”,myClick)
myButton=Button(root,text=“Submit”,command=myClick)
myButton.pack()
bt2=按钮(root,text='RESET',bg='lightblue',command=RESET)
bt2.pack()
root.mainloop()

通常更简单的方法是在启动时创建一次标签,然后在需要时更改其文本-
mySubmit[“text”]=result()
例如,或者
mySubmit.config(text=result())
也是@jasonharper所说的一个选项,所以要清楚,无论用户输入了多少输入,每次只能有一个文本,并且当调用
reset()
时,文本应该消失吗?