cover-img

Tic Tac Toe 🎮 with Python tkinter - part 1

3 May, 2022

18

18

0

In this tutorial, we will be creating a basic Tic Tac Toe game 🎮 with Python tkinter. If you are new to tkinter, refer this crash course: https://www.showwcase.com/show/13940/getting-started-with-tkinter-crash-course
Webpage version: https://www.showwcase.com/show/14592/tic-tac-toe-with-html-css-and-js-part-1

The GUI 👀

Let's go ahead and create a GUI for the game.
Step 1: Create a tkinter window


Screenshot of a basic tkinter window
Step 2: Add play area and a label with text "Tic Tac Toe"


screenshot of a tkinter window with tic tac toe
Step 3: Make the GUI functional
Let's make the GUI functional by changing button text and color when clicked. For this, we need to make some changes to class "XOPoint" in code.


Tic Tac Toe functional tkinter GUI

Detect win and draw 🤔

Let's now implement a logic to detect win/draw.
Step 4: Implement logic to detect win
We need to check after each move if X or O won the game. There are 8 possible ways in which one can win Tic Tac Toe:

8 possible ways of winning Tic Tac Toe
Let's modify the code to detect game win.


Step 5: Detect draw
If all 9 squares were filled and still no one won the game, it's a draw! Detecting game draw is simple. Just append the following code to the function "check_win".


Enhancements

Now that everything works fine, we can improve it further.
Step 6: Status Label
Tic Tac Toe is a GUI game and displaying game result in GUI is better than printing them in console. So, let's create a tkinter Label via which we can display result. Add the following code:


Also, we need to make some changes to function "check_win":


Python tkinter Tic Tac Toe working demo
Step 7: Display whose turn it is
The status label is not used till someone wins the game. Let's use it to display if it's X's turn or O's turn! We need to make some changes to function "set" in class "XOPoint".


Also, the text property of status label must be changed:


Python tkinter Tic Tac Toe working demo
Step 8: Create Play again button
Once the game is over, the game needs to be disabled and a button to play the game again must be displayed. Let's add the following code to disable the game once it is over:


We need to make changes to the function "check_win" and to the for loops that create "XOPoint" objects:


Python tkinter Tic Tac Toe working demo
Add the following code to display Play again button once the game is over:


We also need to make changes to function "disable_game" and function "reset" in class "XOPoint":


Python tkinter Tic Tac Toe working demo

Summary

The steps done in this tutorial are:

  1. Create a tkinter window.
  2. Add play area and a label with text "Tic Tac Toe".
  3. Make the GUI functional.
  4. Implement logic to detect win.
  5. Detect draw.
  6. Status Label.
  7. Display whose turn it is.
  8. Create Play again button.

Final code:


GitHub repo link: https://github.com/Jothin-kumar/tic-tac-toe Python tkinter Tic Tac Toe working demo
If you find this article useful, drop a like ⭐ and follow me to get all my latest content.
Part 2 of this tutorial: https://www.showwcase.com/show/14918/tic-tac-toe-with-python-tkinter-part-2

python

tutorial

tictactoe

tkinter

beginner

18

18

0

python

tutorial

tictactoe

tkinter

beginner

Jothin Kumar
Pythonista || Open source enthusiast.

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2025. Showcase Creators Inc. All rights reserved.