pythonlisttkinteroptionmenu

Updating optionMenu when pressed


I searched all over the internet, and couldn't find a way to update the options menu when it is pressed. I should have a method that will run after the optionsMenu is pressed, like an actual button, such that the options are immediately updated according to the above method.
Is this even possible? if there isn't such a way, is there a good alternative for this over Tkinter, which looks as good as optionsMenu?

Thanks!


Solution

  • Instead of tkinter.OptionMenu use tkinter.ttk.Combobox, to modify the list of values of a combobox simply change it's values attribute like so -:

    def add_to_combobox(new_item) :
        combobox['values'].append(new_item)
    
    combobox = tkinter.ttk.Combobox(master, option=value, ...)
    combobox['values'] = ['my', 'new', 'list']
    combobox.current() # Can pass as argument the index of the list to set as default
    

    Now, to add a new item to the combobox simply call the add_to_combobox method with the new_item to insert.