2

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 file name is selected to show some data from the specific file as option.To be clear,my question is only about how to make the option menu change when a choice from the drop down is selected.Thanks for any help.

2 Answers 2

10

The OptionMenu widget is nothing more than a convenience class that creates a menubutton that is associated with a menu. You can get at this menu via the "menu" attribute. The only trick is to knowing what the menu items should do, which is nothing more than setting the value of the associated variable.

Here's an example:

import Tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.om_variable = tk.StringVar(self)

        b1 = tk.Button(self, text="Colors", width=8, command=self.use_colors)
        b2 = tk.Button(self, text="Sizes", width=8, command=self.use_sizes)

        self.om = tk.OptionMenu(self, self.om_variable, ())
        self.om.configure(width=20)
        self.use_colors()

        b1.pack(side="left")
        b2.pack(side="left")
        self.om.pack(side="left", fill="x", expand=True)


    def _reset_option_menu(self, options, index=None):
        '''reset the values in the option menu

        if index is given, set the value of the menu to
        the option at the given index
        '''
        menu = self.om["menu"]
        menu.delete(0, "end")
        for string in options:
            menu.add_command(label=string, 
                             command=lambda value=string:
                                  self.om_variable.set(value))
        if index is not None:
            self.om_variable.set(options[index])

    def use_colors(self):
        '''Switch the option menu to display colors'''
        self._reset_option_menu(["red","orange","green","blue"], 0)

    def use_sizes(self):
        '''Switch the option menu to display sizes'''
        self._reset_option_menu(["x-small", "small", "medium", "large"], 0)

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()
Sign up to request clarification or add additional context in comments.

3 Comments

So, are you saying in order to dynamically add menu items to an already instantiated menu that you have to delete the menu items before you can make new ones?
@user2962794: no, that's not quite what I'm saying. There's no requirement to delete the items that are there. At its heart it's just a menu, and you have full access to all menu methods. You can insert new items, and delete or modify existing items.
Sorry. I figured it out. I had some bug or something in my code that prevented the dynamic menu items from appearing (and so I found this question, and asked for clarification). It works as I had originally hoped (you don't have to add the menu items in the menu creation, I mean). However, it's good to hear the clarification! :)
-1

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,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.