Cole
Cole

Reputation: 359

Can you assign more than one command to a Tkinter OptionMenu?

I have the following OptionMenu:

    self.wcartsn = StringVar(self.frame1)                                                  
    self.e1 = OptionMenu(self.frame1, self.wcartsn, *watercarts, command=(self.wcart, lambda selection:self.other_entry(selection,'wcartsn',10,6)))   
    self.e1.grid(row=10, column=5, stick=E+W)

This doesn't actually work, but it makes my question clear. How (if possible) do you call multiple functions from one OptionMenu? This function gives the error TypeError: 'tuple' object is not callable

Upvotes: 0

Views: 678

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 385980

The simplest solution -- and the one that is easiest to maintain -- is to create a single function that calls the other functions:

def __init__(self):
    ...
    self.wcartsn = StringVar(self.frame1)                                                  
    self.e1 = OptionMenu(self.frame1, self.wcartsn, *watercarts, command=self.wcartsnCallback)   
    ...

def wcartsnCallback(self, selection):
    self.wcart()
    self.other_entry(selection,'wcartsn',10,6)

Upvotes: 0

Kevin
Kevin

Reputation: 76194

You could create a function that calls each of your multiple functions in turn:

def compose(functions):
    """
    returns a single function composed from multiple functions.
    calling the returned function will execute each of the functions in the order you gave them.
    """
    def f(*args, **kargs):
        for function in functions:
            function(*args, **kargs)
    return f


self.e1 = OptionMenu(self.frame1, self.wcartsn, *watercarts, command=compose(self.wcart, lambda selection:self.other_entry(selection,'wcartsn',10,6)))   

Upvotes: 1

Related Questions