Search code examples
pythonpython-2.7tkinteroptionmenu

Destroy an OptionMenu in Python


I seem to be hitting a dead end here. I have done a decent amount of research online and have not been able to reach a solution.

My issue is, i have an "optionmenu" (#1) in my GUI, when a certain option is chosen, a new "optionmenu" (#2) is created. The user can then make his choice in #2. Based on his choice in #2, entry widgets appear and are destroyed as the option is changed. My problem is here, when optionmenu #2 is displayed and the user decides to change optionmenu#1, i am able to destroy all the entry widgets from the #1 and #2 optionmenu; however, i am still left with the optionmenu#2 in the background.

I was only able to find online solutions for

Entry & Label

However, i was unable to find any solution for

OptionMenu

Any ideas on how to destroy the option menu? A snippet of the code is below, as it currently behaves as stated above.

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

Question amended after Bryan clarification.


Solution

  • Thanks to @BryanOakley hint of the variable and widget not being called on. By placing the .grid on a new line and calling on the widget and appending it seemed to resolve the issue.

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