mardi 22 avril 2014

Python 2.7 - peut attribuer plus d'une commande à un Tkinter OptionMenu ? -Débordement de pile


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




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


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



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

0 commentaires:

Enregistrer un commentaire