title: PyQt/Pyside - Create & Connect Dynamically - capturing function and arguments in lambda
body: <p>I'm trying to recreate the windows note pad.</p>
  
  <p>I'm currently messing with the <code>QMenuBar</code></p>
  
  <p>I made a dictionary with all the menus and actions it will have in the following pattern:</p>
  
  <pre><code>menus = {'File':[['New', 'Ctrl+n'],
                   ['Open', 'Ctrl+o'],
                   ['Save', 'Ctrl+s'],
                   ['Save as...', None],
                   'Separator', 
                   # and so on
  </code></pre>
  
  <p>Then I iterate over that dict, and created the menus and actions successfully and stored them in a second dictionary.</p>
  
  <p>Now I'm trying to connect each <code>action[new, open, save, …]</code> to a instance method of the same name.</p>
  
  <p>I'm doing like so:</p>
  
  <pre><code>for action in menus[m]:
      action = menu.addAction(action[0])
  
      if action[1]:
          action.setShortcut(QKeySequence(action[1]))
  
      if isinstance(action, QAction):
          fname = action[0].lower() 
          # and some other string manipulations
          func = getattr(self,fname)
          action.triggered.connect(lambda arg=action: func(arg))
  </code></pre>
  
  <p>It connects successfully, but if I try to use any of the actions it does nothing.</p>
  
  <p>I had previously connected actions using lambda functions, but this is the first time I'm using <code>getattr()</code> and lambda together.</p>