销毁Python中的OptionMenu

销毁Python中的OptionMenu,python,python-2.7,tkinter,optionmenu,Python,Python 2.7,Tkinter,Optionmenu,我在这里似乎走到了死胡同。我在网上做了大量的研究,还没有找到解决办法 我的问题是,我的GUI中有一个“optionmenu”(#1),当选择某个选项时,会创建一个新的“optionmenu”(#2)。然后用户可以在#2中进行选择。根据他在#2中的选择,条目小部件会随着选项的更改而显示和销毁。我的问题是,当显示optionmenu#2并且用户决定更改optionmenu#1时,我能够销毁#1和#2 optionmenu中的所有条目小部件;但是,我仍然在后台保留选项菜单2 我只能在网上找到解决方案

我在这里似乎走到了死胡同。我在网上做了大量的研究,还没有找到解决办法

我的问题是,我的GUI中有一个“optionmenu”(#1),当选择某个选项时,会创建一个新的“optionmenu”(#2)。然后用户可以在#2中进行选择。根据他在#2中的选择,条目小部件会随着选项的更改而显示和销毁。我的问题是,当显示optionmenu#2并且用户决定更改optionmenu#1时,我能够销毁#1和#2 optionmenu中的所有条目小部件;但是,我仍然在后台保留选项菜单2

我只能在网上找到解决方案

条目和标签

然而,我找不到任何解决问题的办法

选项菜单

关于如何销毁选项菜单有什么想法吗?下面是代码片段,因为它当前的行为与上面所述的一样

from Tkinter import *

neper_tessellation_type={'hard-core':'1','centroid':'3','weight':'0'}
neper_weight_type={'dirac':'1','gaussian':'2','flat':'2','bernoulli':'3'}

class Application(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
     #Setting up widgets onLoad
        self.grid()
        self.create_widgets()

# Tessellations Type Option Builder
    def tessellation_type(self, req_dim):
        global tess_container
        global labels_container
        global weight_container

        for wid in tess_container:
            wid.destroy() ## Destroy the OPTIONMENU1 ENTRY CONTAINER fields

        for label in labels_container:
            label.destroy() ## Supposed to destroy the  OptionMenu2 ITSELF, but does not do as requried.

        for lid in weight_container:
            lid.destroy() ## Destroy the OPTIONMENU2 ENTRY CONTAINER fields

        weight_container = []
        labels_container = []
        tess_container = []

        for type, req_dim in neper_tessellation_type.iteritems():
            self.s = StringVar()
            choice = self.tess_type.get()
            if type == self.tess_type.get() and choice != 'weight':
                u = int(req_dim)
            elif choice == 'weight': ## OPTIONMENU 2 - When weight is chosen a new drop down menu is made and the function command moves to weighttype
                weight_dropdown = OptionMenu(self, self.s, *neper_weight_type, command=self.weight_type).grid(row=13, column=2)
                u = 0
        for b in range(u):
            c = Entry(self)
            c.grid(row=13, column=2 + b)
            tess_container.append(c)  # Append widget to container list

    def weight_type(self, req_dim1):
        global weight_container

        for lid in weight_container:
            lid.destroy()

        weight_container = []

        for type1, req_dim1 in neper_weight_type.iteritems():
            if type1 == self.s.get():
                u1 = int(req_dim1)

        for bf in range(u1):
            t = Entry(self)
            t.grid(row=13, column=3 + bf)
            weight_container.append(t)  # Append widget to container list

# *** MAIN FRAMES ***

    def create_widgets(self):
## OPTIONMENU 1
        Label(self, text="Voronoi Type").grid(row=13, column=0)
        self.tess_type = StringVar()
        tess_type_dropdown = OptionMenu(self, self.tess_type, *neper_tessellation_type, command=self.tessellation_type).grid(row=13, column=1)

## Reset for containers of choice
tess_container = []
labels_container = []
weight_container = []
root = Tk()
app = Application(root)
root.mainloop()

问题在Bryan澄清后修正。

感谢@BryanOakley提示变量和小部件未被调用。通过将.grid放在新行上,调用小部件并附加它,似乎解决了这个问题

...
elif choice == 'weight':
    self.weight_dropdown = OptionMenu(self.tessframe, self.s, *neper_weight_type, command=self.weight_type)
    self.weight_dropdown.grid(row=13, column=2)
    labels_container.append(self.weight_dropdown)
    u = 0
...

有没有可能把你想销毁的所有东西放在一个单独的框架中,然后销毁这个框架。请注意,所有小部件都继承了销毁方法AFAIK。您是否在其上尝试过destroy()?+“destroy函数的工作原理是创建一个小部件容器,然后销毁该容器”\。不,销毁功能不是这样工作的。destroy函数只是删除调用它的小部件。它对OptionMenu的作用与对Label或Entry小部件的作用相同,您不需要所有这些代码来重现问题。请阅读,然后尽可能减少代码。@CurlyJoe这些框架与用户手册一致,修改它们会很麻烦。
labels\u container
是空的,所以它甚至从未试图破坏任何东西。您可以通过非常基本的调试发现这一点。
destroy()。