VOOZH about

URL: https://thenewstack.io/tic-tac-toe-with-python-and-tkinter/

⇱ Tic-Tac-Toe with Python and Tkinter - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2023-11-29 09:46:18
Tic-Tac-Toe with Python and Tkinter
Python / Software Development / Tech Culture

Tic-Tac-Toe with Python and Tkinter

Learn to build a tic-tac-toe app in Python in about 30 minutes.
Nov 29th, 2023 9:46am by Jessica Wachtel
👁 Featued image for: Tic-Tac-Toe with Python and Tkinter
Image via Pixabay.

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:

👁 Image

This project uses Python and Tkinter. Tkinter is Python’s standard GUI package and comes with the Python language install.

We only need to import two Python modules.

Let’s define our functions so we can code without getting blocked by errors. The “`pass“` 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() “`.  Adding “`board.title(“Tic-Tac-Toe”)“` 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”]“`. Add “`board.mainloop()“` 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 “`command=restart“` 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:

👁 Image

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, “`next_turn()“`.

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:

👁 Image

Step 2: Add the logic

The “`next_turn()“` function is the first logic function that we’ll build out. The purpose of the “`next_turn()“` 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.)

The “`check_winner()“` 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:

👁 Image

To identify ties, we’ll need to build the “`empty_spaces()“` function. When called, the “`empty_spaces()“` 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 “`spaces“` variable.

With ties added, the board will look like this:

👁 Image

The last thing we need to do before the project is complete is build the “`restart()“` 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:

TRENDING STORIES
Jessica Wachtel is a developer marketing writer at InfluxData where she creates content that helps make the world of time series data more understandable and accessible. Jessica has a background in software development and technical journalism.
Read more from Jessica Wachtel
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.