在 Python 中销毁一个 OptionMenu

Destroy an OptionMenu in Python

我这里好像走入了死胡同。我在网上做了很多研究,但未能找到解决方案。

My issue is, i have an "optionmenu" (#1) in my GUI, when a certain option is chosen, a new "optionmenu" (#2) is created.然后用户可以在#2 中做出选择。根据他在 #2 中的选择,条目小部件会随着选项的更改而出现和销毁。我的问题在这里,当显示选项菜单#2 并且用户决定更改选项菜单#1 时,我能够从#1 和#2 选项菜单中销毁所有条目小部件;然而,我仍然在后台留下选项菜单#2。

我只能找到

的在线解决方案

Entry & Label

但是,我找不到

的任何解决方案

OptionMenu

关于如何破坏选项菜单的任何想法?下面是一段代码,因为它目前的行为如上所述。

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
...