RickTee
27th January 2020, 11:26
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.
GRID_SIZE = 3
# Create our board of 9 buttons and put them in a grid
for i in range(0, GRID_SIZE ):
for j in range(0, GRID_SIZE ):
self.button[i][j] = QPushButton()
self.button[i][j].setMinimumSize(25, 25)
self.button[i][j].setMaximumSize(25, 25)
grid.addWidget(self.button[i][j], i, j)
vbox.addLayout(grid)
# Add the warnings label
vbox.addWidget(self.labelWarn)
vbox.addStretch(0)
hbox.addLayout(vbox)
hbox.addStretch(0)
self.setLayout(hbox)
self.button[0][0].clicked.connect(lambda: self.on_button_clicked(0, 0))
self.button[0][1].clicked.connect(lambda: self.on_button_clicked(0, 1))
self.button[0][2].clicked.connect(lambda: self.on_button_clicked(0, 2))
self.button[1][0].clicked.connect(lambda: self.on_button_clicked(1, 0))
self.button[1][1].clicked.connect(lambda: self.on_button_clicked(1, 1))
self.button[1][2].clicked.connect(lambda: self.on_button_clicked(1, 2))
self.button[2][0].clicked.connect(lambda: self.on_button_clicked(2, 0))
self.button[2][1].clicked.connect(lambda: self.on_button_clicked(2, 1))
self.button[2][2].clicked.connect(lambda: self.on_button_clicked(2, 2))
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.
GRID_SIZE = 3
# Create our board of 9 buttons and put them in a grid
for i in range(0, GRID_SIZE ):
for j in range(0, GRID_SIZE ):
self.button[i][j] = QPushButton()
self.button[i][j].setMinimumSize(25, 25)
self.button[i][j].setMaximumSize(25, 25)
grid.addWidget(self.button[i][j], i, j)
vbox.addLayout(grid)
# Add the warnings label
vbox.addWidget(self.labelWarn)
vbox.addStretch(0)
hbox.addLayout(vbox)
hbox.addStretch(0)
self.setLayout(hbox)
self.button[0][0].clicked.connect(lambda: self.on_button_clicked(0, 0))
self.button[0][1].clicked.connect(lambda: self.on_button_clicked(0, 1))
self.button[0][2].clicked.connect(lambda: self.on_button_clicked(0, 2))
self.button[1][0].clicked.connect(lambda: self.on_button_clicked(1, 0))
self.button[1][1].clicked.connect(lambda: self.on_button_clicked(1, 1))
self.button[1][2].clicked.connect(lambda: self.on_button_clicked(1, 2))
self.button[2][0].clicked.connect(lambda: self.on_button_clicked(2, 0))
self.button[2][1].clicked.connect(lambda: self.on_button_clicked(2, 1))
self.button[2][2].clicked.connect(lambda: self.on_button_clicked(2, 2))