3

I'm trying to create a simple keyboard from list of QtGui.QPushButton objects.

class XKeyboard(QtGui.QWidget):
  '''Special virtual keyboard for any language.'''
  def __init__(self, parent=None):
    QtGui.QWidget.__init__(self, parent)
    self.MainLayout = QtGui.QVBoxLayout()
    self.TextEntry = QtGui.QTextEdit()
    self.Keyboard = QtGui.QVBoxLayout()
    self.MainLayout.addWidget(self.TextEntry)
    self.MainLayout.addLayout(self.Keyboard)
    self.setLayout(self.MainLayout)

  def addRow(self, keys):
    layout = QtGui.QHBoxLayout()
    buttons = [QtGui.QPushButton(unicode(key)) for key in keys]
    for button in buttons:
      key = keys[buttons.index(button)]
      layout.addWidget(button)
      button.clicked.connect(
          lambda key: self.keyClick(key))
      self.keyClick(key)
    self.Keyboard.addLayout(layout)

  def keyClick(self, key):
    self.TextEntry.insertPlainText(key)

The problem is that lambda returns False instead of key. What am I doing wrong?

0

2 Answers 2

5

That lambda :

lambda key: self.keyClick(key)

is equivalent to this:

def dummy(key):
    self.keyClick(key)

So, basically you are accepting a parameter from clicked signal, which returns checked state of the button and it is False since you don't have a check-able button, and passing it to the keyClick method. keyClick method does not receive the key parameter in the for loop.

Possible solution would be writing your lambda accepting two parameters one with the default value of your intended value:

lambda checked, key=key: self.keyClick(key)

Why you need to do key=key is a whole different subject. This particular answer (along with the other answers) from a related subject might shed some light on it.

2
  • Yeah, man, that works! Only one question: if I generate signal that doesn't return any state (like triggered()), do I still need lambda checked, key=key or I can write lambda key=key?
    – ghostmansd
    Jan 23, 2012 at 22:52
  • @ghostmansd: You should (not "can") write lambda key=key, otherwise you'll get and exception stating insufficient parameters. I put checked there because you need to discard the parameter passed from clicked.
    – Avaris
    Jan 23, 2012 at 22:59
3

The right way to do this is to use a QButtonGroup, which is designed for precisely this situation.

Here's one way to re-write your example to use it:

class XKeyboard(QtGui.QWidget):
  '''Special virtual keyboard for any language.'''
  def __init__(self, parent=None):
    QtGui.QWidget.__init__(self, parent)
    self.MainLayout = QtGui.QVBoxLayout()
    self.TextEntry = QtGui.QTextEdit()
    self.Keyboard = QtGui.QVBoxLayout()
    self.MainLayout.addWidget(self.TextEntry)
    self.MainLayout.addLayout(self.Keyboard)
    self.setLayout(self.MainLayout)
    self.buttonGroup = QtGui.QButtonGroup(self)
    self.buttonGroup.buttonClicked[int].connect(self.keyClick)

  def addRow(self, keys):
    layout = QtGui.QHBoxLayout()
    for key in keys:
        key = unicode(key)
        button = QtGui.QPushButton(key)
        self.buttonGroup.addButton(button, ord(key))
        layout.addWidget(button)
    self.Keyboard.addLayout(layout)

  def keyClick(self, key):
    self.TextEntry.insertPlainText(unichr(key))

Not the answer you're looking for? Browse other questions tagged or ask your own question.