Tic-Tac-Toe with Python and Tkinter
Welcome to Python Tic-Tac-Toe. This is a great way to get your hands on some Python code. This project includes just enough logic and control flow to be challenging but not so much that it will take hours of hard work. In about 30 minutes, give or take depending on experience level, you’ll have at least one finished project. The end result will look like this:
We only need to import two Python modules.
Let’s define our functions so we can code without getting blocked by errors. The
keyword is a placeholder and will let us move on without writing any additional code in the functions.
Step 1: Build the Board
The first step is our Tkinter heavy step because the Tkinter module gives the board its shape and functionality.
We define our board as an instance of the Tkinter frame. This translates to code as
board = Tk()
to the code file titles the board appropriately.
The next step is to create a list of characters. My list is going to include an “x” and an “o”. My code will look like this
characters = ["x", "o"]
at the bottom of the code file. This will remain as the last line of code. This keeps the game board open and listening for events.
At this point, my code file looks like this:
Next, let’s define the player variable which determines whose turn it is. The player variable uses the random Python module to pick the starting player at random.
player = random.choice(characters)
We create the buttons with a two-dimensional array or a list of lists. Since there are no values yet for the buttons, I am going to assign each button the value of an empty space. You can assign each button any value you’d like within this two-dimensional array since we’re going to reassign values when we create button values later in this process.
Let’s create the status bar letting each player know whose turn it is and the new game button. Tkinter’s Label widget will create the status bar.
The new game button will look like the code below. The
section of this code is what will trigger the new game to start any time a player clicks the button.
We also need to create a frame that will assign each button a correct location on the board. That code looks like this:
And the board will look like this:
Now it’s time to build the buttons. Building the buttons requires the use of nested loops. The outer loop defines the row index. The inner loop defines the column index. The code block inside the loops builds the buttons, assigns them the necessary attributes, and adds them to the grid. The buttons have two functions. The first is a lambda function which assigns the value of its row index and column index to variables also named row and column that can be passed along to the second function,
The inner and outer loops have a hard-coded range of 3 because the board’s rows and columns include three squares each. Each row and column start at index 0.
At the end of the board-building stage, your code file will look like this:
And the board will look like this:
Step 2: Add the logic
function is the first logic function that we’ll build out. The purpose of the
function is to check for a winner. If there’s no winner, then the next player gets a turn. But how does that actually work? (Don’t forget to add the row/column parameters to the function.)
function is next. In this function, we want to check each row, column, and diagonal for a winner or tie. That translates to the following code:
At this point, you will be able to click on the squares and see characters appear and account for a winner. The board will look like this:
To identify ties, we’ll need to build the
function. When called, the
function will check to see if the button’s text is blank or is a character. If the text is a character then the function will decrement from the total count of the
With ties added, the board will look like this:
The last thing we need to do before the project is complete is build the
function. This function will update the global player variable, adjust the player status bar text, and reset all the button text back to blank spaces.
That completes this tutorial on Python Tic-Tac-Toe! This tutorial is closely matched with this video tutorial presented by Bro Code because they did a great job explaining the how’s and why’s that I just couldn’t do in text. Check out the video for more learning.
Here’s a full copy/paste of the code file: