1

Is there a way to make an options menu perform a command from a range of different commands?

from tkinter import *
#creates 2 windows
root = Tk()
popup = Toplevel()

#places frame in popup window
popup_frame = Frame(popup)
popup_frame.pack()

#4 functions, text is just placeholder for idea
def popup1(popup):
    popup_frame.destroy()
    popup_frame = Frame(popup)
    label_popup1 = Label(popup_frame, text="unique textfor popup1")
    popup_frame.pack()
    label_popup1.pack()

def popup2(popup):
    popup_frame.destroy()
    popup_frame = Frame(popup)
    label_popup2 = Label(popup_frame, text="unique text for popup2")
    popup_frame.pack()
    label_popup2.pack()

def popup3(popup):
    popup_frame.destroy()
    popup_frame = Frame(popup)
    label_popup3 = Label(popup_frame, text="unique text for popup3")
    popup_frame.pack()
    label_popup3.pack()

def popup1(popup):
    popup_frame.destroy()
    popup_frame = Frame(popup)
    label_popup4 = Label(popup_frame, text="unique text for popup4")
    popup_frame.pack()
    label_popup4.pack()

#2 option menus offering different selections
options1 = ["popup1", "popup2"]
variable1 = StringVar()
drop_title1 = StringVar(root)
#sets initial name for widget
drop_title1.set("Please Choose Options 1 Or 2")
dropdownmenu1 = OptionMenu(root, drop_title1, *options, command=lambda:func())
dropdownmenu1.place(x=10, y=10)

options2 = ["popup3", "popup4"]
variable2 = StringVar()
drop_title2 = StringVar(root)
drop_title2 .set("Please Choose Options 3 Or 4")
dropdownmenu2 = OptionMenu(root, drop_title2, *options, command=lambda:func())
dropdownmenu2.place(x=100, y=10)

root.mainloop()

So there is the root frame with 2 different dropdown menus in it, where if you clicked on say option 1, the popup_frame would be deleted and replaced with the contents of option 1, then from the second drop-down menu, option 4 is selected and the contents of the popup_frame change to what option 4 contains?

Twisted Fate
  • 145
  • 1
  • 14

1 Answers1

2

The function defined as command for the OptionMenu will be given the selected value as argument. So you can define the callback function to check that value and call another function depending on which value it is:

from tkinter import *
#creates 2 windows
root = Tk()
popup = Toplevel()

#places frame in popup window
popup_frame = Frame(popup)
popup_frame.pack()

#4 functions, text is just placeholder for idea
def popup1():
    global popup_frame
    popup_frame.destroy()
    popup_frame = Frame(popup)
    label_popup1 = Label(popup_frame, text="unique text for popup1")
    popup_frame.pack()
    label_popup1.pack()

def popup2():
    global popup_frame
    popup_frame.destroy()
    popup_frame = Frame(popup)
    label_popup2 = Label(popup_frame, text="unique text for popup2")
    popup_frame.pack()
    label_popup2.pack()

def popup3():
    global popup_frame
    popup_frame.destroy()
    popup_frame = Frame(popup)
    label_popup3 = Label(popup_frame, text="unique text for popup3")
    popup_frame.pack()
    label_popup3.pack()

def popup4():
    global popup_frame
    popup_frame.destroy()
    popup_frame = Frame(popup)
    label_popup4 = Label(popup_frame, text="unique text for popup4")
    popup_frame.pack()
    label_popup4.pack()

def func(val):
    if val == "popup1":
        popup1()
    elif val == "popup2":
        popup2()
    elif val == "popup3":
        popup3()
    elif val == "popup4":
        popup4()

#2 option menus offering different selections
options1 = ["popup1", "popup2"]
variable1 = StringVar()
drop_title1 = StringVar(root)
#sets initial name for widget
drop_title1.set("Please Choose Options 1 Or 2")
dropdownmenu1 = OptionMenu(root, drop_title1, *options1, command=func)
dropdownmenu1.pack(side=LEFT)

options2 = ["popup3", "popup4"]
variable2 = StringVar()
drop_title2 = StringVar(root)
drop_title2 .set("Please Choose Options 3 Or 4")
dropdownmenu2 = OptionMenu(root, drop_title2, *options2, command=func)
dropdownmenu2.pack(side=LEFT)

root.mainloop()
fhdrsdg
  • 10,297
  • 2
  • 41
  • 62
  • Always been told to avoid global variable as they cause issues, but either way thanks, It does what I need it too. – Twisted Fate May 24 '18 at 11:40
  • Well yeah, but when you want to manipulate a variable that's created in the global namespace from a function you have to. You could put your GUI in a Class, which lets you save the widgets as class variables and avoid global variables. See [this answer](https://stackoverflow.com/a/17470842/3714930) for a nice template to do this. – fhdrsdg May 24 '18 at 12:34