0

I need to pass two variables to a function defined in a lambda, that is executed in a loop, but I'm sure how to bind more than one variable. I've seen answers on here before where they bind a variable to capture it during the current iteration of the loop - like shown here: Python Lambda in a loop, but I don't know how to do it in my situation:

def on_click(x, y):
    buttons[(x, y)].setStyleSheet("QPushButton {\n"
        "background-color: black;\n"
        "height: 20px;\n"
        "width: 20px}")
    print(x, y)

for i in range(rows):
    for j in range(cols):
        # keep a reference to the buttons
        buttons[(i, j)] = QtWidgets.QPushButton()
        buttons[(i, j)].setStyleSheet("QPushButton {\n"
        "background-color: gray;\n"
        "height: 20px;\n"
        "width: 20px}")
        buttons[(i, j)].clicked.connect(lambda x=i, y=j: on_click(x, y))
        # add to the layout
        layout.addWidget(buttons[(i, j)], i, j)

0 Answers0