Skip to content Skip to sidebar Skip to footer

Linking Two OptionMenu Widgets Tkinter

I have two OptionMenu widgets in the simple pieces of code shown below: variable = StringVar(win1) variable.set(number(number2)) type = O

Solution 1:

It's not exactly clear what you want, but I think this should do it.

When "Heavy" is selected in the first menu, "colour" is selected in the second one and that menu is disabled (can't select anything else). When something else is selected in the first menu, the second one goes to "mm" and is enabled again.

from Tkinter import *

class app:
    def __init__(self, root):
        win1 = Frame(root)
        win1.grid(row=0,column=0)

        self.variable = StringVar(win1)                               
        self.variable.set(42)
        self.type = OptionMenu(win1, self.variable,
                          "None", "Clear", "Dark", "Heavy",
                          command = self.varMenu)
        self.type.grid(row=1, column=3, sticky="nsew", padx=1, pady=1)


        self.variableunit = StringVar(win1)
        self.variableunit.set('mm')
        self.unit = OptionMenu(win1,
                          self.variableunit, "mm", "colour", "shade")
        self.unit.grid(row=1, column=5, sticky="nsew", padx=1, pady=1)

    def varMenu(self, selection):
        if selection == "Heavy":
            self.variableunit.set("colour")
            self.unit.config(state = DISABLED)
        else:
            self.variableunit.set("mm")
            self.unit.config(state = NORMAL)

root = Tk()
a = app(root)
root.mainloop()

Post a Comment for "Linking Two OptionMenu Widgets Tkinter"