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?
That
lambda:is equivalent to this:
So, basically you are accepting a parameter from
clickedsignal, which returnscheckedstate of the button and it isFalsesince you don’t have a check-able button, and passing it to thekeyClickmethod.keyClickmethod does not receive thekeyparameter in the for loop.Possible solution would be writing your
lambdaaccepting two parameters one with the default value of your intended value:Why you need to do
key=keyis a whole different subject. This particular answer (along with the other answers) from a related subject might shed some light on it.