0

I am trying create a grid of buttons (like minesweeper) and I am trying to set a custom attribute that records the location on the grid (column number and row number) - I would like to set the value as it is when b.cell_num is first set, however calling it later (when the button is pressed) causes it to just return whatever the final values of col_num and row_num are (e.g. for a 10x12 grid, all buttons just print "(9, 11)".

I appreciate any help! The relevant code: (board.game_board is just an array - a list of lists)

def draw_board(w, board):
    for row_num, row in enumerate(board.game_board):
        for col_num, cell in enumerate(row):
            b = QPushButton(w)
            b.resize(40, 40)
            b.setText(str(count))
            b.move(col_num*40, row_num*40)
            b.setCheckable(True)
            b.toggle()
            b.cell_num = (col_num, row_num)
            b.clicked.connect(lambda: cell_clicked(b))


def cell_clicked(b):
    print(b.cell_num)

0 Answers0