Creating a game board with Tkinter

I created a board of labels and color them according to which is clicked:

import Tkinter as tk

board = [ [None]*10 for _ in range(10) ]

counter = 0

root = tk.Tk()

def on_click(i,j,event):
    global counter
    color = "red" if counter%2 else "black"
    event.widget.config(bg=color)
    board[i][j] = color
    counter += 1


for i,row in enumerate(board):
    for j,column in enumerate(row):
        L = tk.Label(root,text='    ',bg='grey')
        L.grid(row=i,column=j)
        L.bind('<Button-1>',lambda e,i=i,j=j: on_click(i,j,e))

root.mainloop()

This doesn't do any validation (to make sure that the element clicked is at the bottom for example). It would also be much better with classes instead of global data, but that's an exercise for the interested coder :).


You probably want to create a grid of Buttons. You can style them according to the values in board, and assign a callback that updates the board when clicked.