Issue
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?
Solution
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.
Answered By - Avaris
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.