I have an OptionMenu in my GUI that is being populated by a list. Every time the user runs a certain process the list updates to reflect this. Is there a way to update the OptionMenu based on the list? I've tried self.plotWindow.update()
as per this question but to no avail. However, closing and reopening the window does refresh the OptionMenu as you would expect. Relevant code:
if self.figNum.get() == 'New Figure...':
if self.figList[-1] == 'New Figure...':
self.figList.append(1)
else:
self.figList.append(self.figList[-1]+1)
self.plotWindow.update() #tk.Tk() window
self.i = self.figList[-1]
else:
self.i = self.figNum.get()
The options in an OptionMenu are not bound to the list from which they are created. Therefore, changing the list does not change the OptionMenu, you'll have to update it yourself.
You can do that by getting the OptionMenu's
menu
, and add commands to that. The following example shows how to do this (based on this answer).It shows that, even though the
self.options
list is appended with an option using the 'Add option to list' button, the OptionMenu does not change automatically. To update the OptionMenu, you can use the 'Update option menu' button for this, which callsself.update_option_menu
. This function deletes all options from the OptionMenu, and inserts a new one for each item inself.options
.