0

I have this code for a basic TicTacToe game, using Tkinter:

from tkinter import *
from functools import partial

class Window(Frame):
    def __init__(self, master = None): # init Window class
        Frame.__init__(self, master) # init Frame class
        self.master = master # allows us to refer to root as master
        self.rows = 3
        self.columns = 3
        self.buttonGrid = [[None for x in range(self.rows)] for y in range(self.columns)]
        self.create_window()
        self.add_buttons()
    
    def create_window(self):
        #title
        self.master.title('Tic Tac Toe')
        self.pack(fill = BOTH, expand = 1)
        for i in range(0,3): # allows buttons to expand to frame size
            self.grid_columnconfigure(i, weight = 1)
            self.grid_rowconfigure(i, weight = 1)
    
    def add_buttons(self):
        rows = 3
        columns = 3
        # create a 2d array to put each button in
        # Creates a list containing 3 lists, each of 3 items, all set to None
        for i in range (rows):
            for j in range(columns):
                button_ij = Button(self, textvariable = self.buttonGrid[i][j], command = lambda: self.add_move(i, j))
                # sticky centres buttons and removes space between adjacent buttons
                button_ij.grid(row = i,column = j, sticky =E+W+S+N)
                
    def add_move(self, i,j): # only 2 and 2 passed in as i and j
        self.buttonGrid[i][j] = 'X'
        print(i, j)
        print(self.buttonGrid)

root = Tk() # creating Tk instance

rootWidth = '500'
rootHeight = '500'
root.geometry(rootWidth+'x'+rootHeight)

ticTacToe = Window(root) # creating Window object with root as master

root.mainloop() # keeps program running

In add_buttons, I am adding the buttons into row i, column j. However, when I call add_move, the i and j values always show up as 2, 2 - so only the last element in the last list of buttonGrid gets changed.

Why does this happen? How can I pass other position values into the add_move method?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
StudyAccount
  • 91
  • 1
  • 11
  • 1
    Does this answer your question? [how-to-understand-closure-in-a-lambda](https://stackoverflow.com/questions/28494089) – stovfl May 16 '20 at 17:27
  • Yes, it does! Next time I will read up on a new piece of code (eg lambda) before I use it. Thank you so much! – StudyAccount May 16 '20 at 17:33
  • You could also use `partial(self.add_move, i, j)` as it essentially does the same thing. – Axe319 May 16 '20 at 17:43
  • Does this answer your question? [tkinter creating buttons in for loop passing command arguments](https://stackoverflow.com/questions/10865116/tkinter-creating-buttons-in-for-loop-passing-command-arguments) – Karl Knechtel Aug 20 '22 at 00:47

0 Answers0