在Python中销毁OptionMenu

时间:2016-03-17 19:08:49

标签: python python-2.7 tkinter optionmenu

我似乎在这里遇到了死胡同。我在网上做了大量研究,但未能找到解决方案。

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

我只能找到

的在线解决方案
  

进入&标签

然而,我无法找到

的任何解决方案
  

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()

在布莱恩澄清之后修改了问题。

1 个答案:

答案 0 :(得分:0)

感谢@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
...