Tkinter从Frame中删除/覆盖元素

时间:2017-10-24 09:31:30

标签: python tkinter

我创建了一个按钮,该按钮根据文本字段中的某些输入从DataFrame检索列表。每次按下按钮,列表都会刷新。我在单独的OptionMenu(outputFrame)中输出列表(作为Frame)。但是,每次按此按钮时,OptionMenu都会添加一个新的Frame(而不是覆盖前一个)。每次按下按钮时,如何确保“ouputFrame”的内容被覆盖?

# start
root = Tkinter.Tk()

# frames
searchBoxClientFrame = Tkinter.Frame(root).pack()
searchButtonFrame = Tkinter.Frame(root).pack()
outputFrame = Tkinter.Frame(root).pack()

# text field
searchBoxClient = Tkinter.Text(searchBoxClientFrame, height=1, width=30).pack()

# function when button is pressed
def getOutput():
    outputFrame.pack_forget()
    outputFrame.pack()
    clientSearch = str(searchBoxClient.get(1.0, Tkinter.END))[:-1]
    # retrieve list of clients based on search query
    clientsFound = [s for s in df.groupby('clients').count().index.values if clientSearch.lower() in s.lower()]
    clientSelected = applicationui.Tkinter.StringVar(root)
    if len(clientsFound) > 0:
        clientSelected.set(clientsFound[0])
        Tkinter.OptionMenu(outputFrame, clientSelected, *clientsFound).pack()
    else:
        Tkinter.Label(outputFrame, text='Client not found!').pack()

Tkinter.Button(searchButtonFrame, text='Search', command=getOutput).pack()

root.mainloop()

1 个答案:

答案 0 :(得分:0)

我们实际上可以更新import tkinter as tk root = tk.Tk() var = tk.StringVar(root) choice = [1, 2, 3] var.set(choice[0]) option = tk.OptionMenu(root, var, *choice) option.pack() def command(): option['menu'].delete(0, 'end') for i in range(len(choice)): choice[i] += 1 option['menu'].add_command(label=choice[i], command=tk._setit(var, choice[i])) var.set(choice[0]) button = tk.Button(root, text="Ok", command=command) button.pack() root.mainloop() 本身的值而不是销毁它(或它的父级),然后重新绘制它。感谢this answer以下代码段:

{{1}}