For some reason this sample code on the PySide2 ebook (page 345) is not working
further/signal_extra_3.py
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
v = QVBoxLayout()
h = QHBoxLayout()
for a in range(10):
button = QPushButton(str(a))
# problem with this line
button.clicked.connect(lambda checked, a=a: self.button_clicked(a)) # <1>
h.addWidget(button)
self.label = QLabel("")
v.addLayout(h)
v.addWidget(self.label)
w = QWidget()
w.setLayout(v)
self.setCentralWidget(w)
def button_clicked(self, n):
self.label.setText(str(n))
When I click on each button, I get this error
TypeError: <lambda>() missing 1 required positional argument: 'checked'
The number pressed is not shown on the GUI.
I am running Python 3.8.5 on Win10. This is how I setup my environment
conda create -n pyside2 python=3.8
conda activate pyside2
pip install pyside2, pyqtgraph