Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get text from QPushButton in PyQt [duplicate]

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?

like image 552
ghostmansd Avatar asked Nov 26 '25 19:11

ghostmansd


1 Answers

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.

like image 104
Avaris Avatar answered Nov 30 '25 19:11

Avaris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!