I am using PyQt and want to create a menu based on a list of strings.
The problem is that when I want to call 'addAction', it requires a call-back function (for each string) that does not take any arguments.
For simple menus, this would be fine: e.g.
menu.addAction("Open", self.open)
menu.addAction("Exit", self.quit)
However, I want to just use a single function and have the 'action string' passed in as an argument.
I am wondering if python can do something like this:
def f(x, y):
print x + 2*y
# These 2 variables are of type: <type 'function'>
callback1 = f
callback2 = f(x=7, *)
# NB: the line above is not valid python code.
# I'm just illustrating my desired functionality
print callback1(2,5) # prints 12
print callback2(5) # prints 17
Here is my code snippet:
def printParam(parameter):
print "You selected %s" % parameter
# parameters = list of strings
menu_bar = QMenuBar()
menu = menu_bar.addMenu('Select Parameter')
for parameter in parameters:
# This line will not work because the action does not pass in
# any arguments to the function
menu.addAction(parameter, printParam)
Any suggestions greatly appreciated