Hello fellow qt users.

In the process of learning python I created a tictactoe game and use the PyQt5 to create a gui. Simple 3x3 grid of buttons created in a loop with a view to a 4x4 grid later. Ran into a problem, how to identify the button pressed? Code below shows ugly solution used. Putting clicked.connect statements in the loop fails as it seems the variables i and j are passed by reference so they all contain 2, 2 when the loop exits. Full code here: https://github.com/RickTee/tictacQt. I've probably missed something some were in the docs.

Qt Code:
  1. GRID_SIZE = 3
  2. # Create our board of 9 buttons and put them in a grid
  3. for i in range(0, GRID_SIZE ):
  4. for j in range(0, GRID_SIZE ):
  5. self.button[i][j] = QPushButton()
  6. self.button[i][j].setMinimumSize(25, 25)
  7. self.button[i][j].setMaximumSize(25, 25)
  8. grid.addWidget(self.button[i][j], i, j)
  9.  
  10. vbox.addLayout(grid)
  11. # Add the warnings label
  12. vbox.addWidget(self.labelWarn)
  13. vbox.addStretch(0)
  14. hbox.addLayout(vbox)
  15. hbox.addStretch(0)
  16. self.setLayout(hbox)
  17.  
  18. self.button[0][0].clicked.connect(lambda: self.on_button_clicked(0, 0))
  19. self.button[0][1].clicked.connect(lambda: self.on_button_clicked(0, 1))
  20. self.button[0][2].clicked.connect(lambda: self.on_button_clicked(0, 2))
  21. self.button[1][0].clicked.connect(lambda: self.on_button_clicked(1, 0))
  22. self.button[1][1].clicked.connect(lambda: self.on_button_clicked(1, 1))
  23. self.button[1][2].clicked.connect(lambda: self.on_button_clicked(1, 2))
  24. self.button[2][0].clicked.connect(lambda: self.on_button_clicked(2, 0))
  25. self.button[2][1].clicked.connect(lambda: self.on_button_clicked(2, 1))
  26. self.button[2][2].clicked.connect(lambda: self.on_button_clicked(2, 2))
To copy to clipboard, switch view to plain text mode