I'm studying PyQt on the Mark Summerfield book, but using python 3 and PyQt5 I need to convert all the code to the new syntax, and I'm having some problems on this example:
import sys
from functools import partial
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Form(QDialog):
def __init__(self, parent=None):
super(Form,self).__init__(parent)
button1 = QPushButton('One')
button2 = QPushButton('Two')
button3 = QPushButton('Three')
button4 = QPushButton('Four')
button5 = QPushButton('Five')
self.label = QLabel('Click a button...')
layout = QHBoxLayout()
layout.addWidget(button1)
layout.addWidget(button2)
layout.addWidget(button3)
layout.addWidget(button4)
layout.addWidget(button5)
layout.addStretch()
layout.addWidget(self.label)
self.setLayout(layout)
button1.clicked.connect(self.one)
#partial for passing runtime argument to function, useful for using one slot for many signals
button2.clicked.connect(partial(self.anyButton,'Two'))
button3.clicked.connect(lambda who='Three': self.anyButton(who))
def one(self):
self.label.setText("You clicked button 'One'")
def anyButton(self,who):
self.label.setText("You clicked button '%s'" %who)
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
When I click on button 1 and 2, the program runs as expected, but when I click on button 3 I get this message: "You clicked button 'False'" while I'd expect "You clicked button 'Three'".
So, I think I'm doing something wrong with the syntax of the lambda function, but I can't see what.
Here the original code for pyqt4 and python 2:
self.connect(button3, SIGNAL("clicked()"),lambda who="Three": self.anyButton(who))