3

Disclaimer: I've read other questions like this already (eg. this one) but didn't find a working solution for me yet (or I just don't understand them :))

When I create a lambda inside a for loop accessing data from the scope of the block I get a pylint warning (cell-var-from-loop) because of the way Python captures work. E.g:

for key, value in data.items():
    button = QtGui.QPushButton('show data')
    button.clicked.connect(lambda: show_data(value))
    table_widget.setCellWidget(1, 1, button)

There are more questions like this but I still don't now how I systematically solve this issue. I tried to provide default values to the lambda like suggested here:

for key, value in data.items():
    button = QtGui.QPushButton('show data')
    button.clicked.connect(lambda v=value: show_data(v))
    table_widget.setCellWidget(1, 1, button)

But when I do it like this weird things happen - while value ought to be a string in my example show_data is being called with a bool.

Am I doing something totally wrong? Should this approach work?

2
  • 1
    The clicked signal sends a checked parameter. Use: button.clicked.connect(lambda chk, v=value: show_data(v)). Commented Jun 10, 2017 at 20:36
  • wow :) can you please turn this into an answer? Commented Jun 11, 2017 at 19:55

1 Answer 1

2

The clicked signal sends a checked parameter. So try:

button.clicked.connect(lambda checked, v=value: show_data(v))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.