How can I dynamic populate an option widget in tkinter depending on a choice from a drop down menu?

前端 未结 2 1581
误落风尘
误落风尘 2020-12-09 07:11

my problem is the following: I have several files and I have made a drop down menu with the names,the next thing I need is an option menu which can be changed whenever a fil

2条回答
  •  抹茶落季
    2020-12-09 07:43

    As the OptionMenu gives a command option, i would suggest to keep reference to the command already given (if any) and pass it to the new options this way:

    attendion, variables based on the previous answer

    def __init__(self, *args, **kwargs):
        ...
        self.command=kwargs['command']
        ...
    
    def _reset_option_menu(options, index=None):
        ...
        menu.add_command(label=string, 
                         command=lambda:self.command(),
                         value=self.om_variable.set(string))                         
            ...
    

    Hope its of any use Btw, really usefull the answer given by Bryan Oakley,

提交回复
热议问题