#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    24
    Rep Power
    0

    Tkinter OptionMenu changing values


    I'm trying to set my Tkinter OptionMenu's after loading a file, but I can't get the options to change. I have looked through many websites but none their code works. Does anyone know how to change a Tkinter OptionMenu's drop-down values after its been created?

    Here is some sample code:

    Code:
    from tkinter import *
    
    root = Tk()
    root.title("My Program")
    root.geometry("400x200")
    root.resizable(0,0)
    
    host_options = StringVar(root)
    host_options.set('EMPTY')
    host_selection = OptionMenu(root, host_options)
    host_selection.config(width=15)
    host_selection.pack(side=TOP, pady=(0, 10))
    
    option = host_selection.children['menu']
    
    root.mainloop()
    Okay so I'm trying to dynamically set new options in the OptionMenu for the user every time the user loads a new file. The host_selection.children['menu'] does not work. I found that on another site. It throws a KeyError when I try to run it.

    How do you change an OptionMenu's options after its been created?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    24
    Rep Power
    0
    Well, I'm using ttk instead of tk, and if I use tk the host_selection['menu'] will work. Although its still messed up, the OptionMenu's selections are there, but they all force you to selection the last option no matter what you pick in the list.

    I guess the only way to do this is to rebuild the OptionMenu completely by destroying the previous menu and building another one that has the correct options each time the file is loaded. That is a messy solution though, and its going to be hard to pack the rebuilt menus properly.

    Does anybody else have any suggestions on how to change the OptionMenu's selections?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    524
    Rep Power
    34
    A quickly cobbled example is below. You have to access the menu in the option menu's dictionary and change that.
    Code:
    from Tkinter import *
    
    master = Tk()
    master.geometry("75x100")
    
    variable = StringVar(master)
    
    w = OptionMenu(master, variable, "one", "two", "three")
    w.pack()
    variable.set("two")
    
    def ok():
        m = w.children['menu']
        m.entryconfig(1, label="Horse")
    
    button = Button(master, text="OK", command=ok)
    button.pack()
    
    Button(master, text="Exit", command=master.quit).pack()
    
    mainloop()
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    24
    Rep Power
    0
    Originally Posted by dwblas
    A quickly cobbled example is below. You have to access the menu in the option menu's dictionary and change that.
    Code:
    from Tkinter import *
    
    master = Tk()
    master.geometry("75x100")
    
    variable = StringVar(master)
    
    w = OptionMenu(master, variable, "one", "two", "three")
    w.pack()
    variable.set("two")
    
    def ok():
        m = w.children['menu']
        m.entryconfig(1, label="Horse")
    
    button = Button(master, text="OK", command=ok)
    button.pack()
    
    Button(master, text="Exit", command=master.quit).pack()
    
    mainloop()
    Thanks dwblas.

    I also dug around in the ttk module and took a look at the OptionMenu class. I found a function in it called "set_menu".

    This is how I ended up change the menu options after creation:

    Code:
    # "values" is a list of options for the menu
    
    host_selection.set_menu(*values)
    I can't believe it was only one line of code.

IMN logo majestic logo threadwatch logo seochat tools logo