你能为Tkinter OptionMenu分配多个命令吗?

时间:2014-03-21 13:16:34

标签: python-2.7 tkinter

我有以下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)

这实际上不起作用,但它使我的问题清楚。 如何(如果可能的话)从一个OptionMenu调用多个函数?此函数提供错误TypeError: 'tuple' object is not callable

2 个答案:

答案 0 :(得分:1)

你可以创建一个函数来依次调用你的多个函数:

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)))   

答案 1 :(得分:0)

最简单的解决方案 - 也是最容易维护的解决方案 - 是创建一个调用其他函数的函数:

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)