Update on the fly a ttk.OptionMenu without affecting its previous callback/command

若如初见. 提交于 2021-01-29 07:26:28

问题


I am doing a large GUI with different ttk.OptionMenu widgets that need to be updated often depending on the interaction with the user. Each time the user selects another value from the menu, there is a function callback by the widget via the command input. The command works fine until I need to update the possible values within the OptionMenu widget. After updating using add_command, my previous callback does not work anymore.

Is there any way of updating the possible inputs of a ttk.OptionMenu without affecting its original callback? Let me put a dummy example to illustrate my problem. My original code would be like

from tkinter import *
from tkinter import filedialog, messagebox, ttk

        
def printing(variable,*args):
    
    print('ok')
      
    if variable: 
        print(variable)

root=Tk()
List=['Madrid', 'Paris', 'Brussels']
variable = StringVar()

optionmenu = ttk.OptionMenu(root,variable,'', *List,
                            command=printing) 
optionmenu.pack()      
root.mainloop() 

Then the user would update the list dynamically, and the menu needs to be updated on the fly, using:

newList = ['Madrid', 'Paris', 'Brussels', 'London']
menu = optionmenu["menu"]
menu.delete(0, "end")
for string in newlist:
    menu.add_command(label=string,
                      command=lambda value=string: variable.set(value))

but doing this operation, the callback printing would not work anymore. Also important to mention that one clicked in one option it should become the value showed by the OptionMenu ( so the StringVar variable is settled to that input)

I hope it is clear


回答1:


There is an internal class _setit in tkinter module which is used by OptionMenu internally to populate the values. You can use this class as below:

from tkinter import filedialog, messagebox, ttk, _setit
...

for value in newList:
    menu.add_command(label=value, command=_setit(variable, value, printing))


来源:https://stackoverflow.com/questions/64262274/update-on-the-fly-a-ttk-optionmenu-without-affecting-its-previous-callback-comma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!