Build your own tic tac toe game in Python in 2025

Developing a Tic Tac Toe game in Python with a GUI is a fantastic way to combine the fundamentals of Python with an introduction to GUI programming. Unlike terminal-based gameplay, creating a graphical version makes the game visually appealing and user-friendly. In this guide, we’ll use the Tkinter library to design and implement a fully functional Tic Tac Toe game.


Outline of the Article

  1. Introduction
  2. Why Build a GUI-Based Tic Tac Toe Game in Python?
  3. What is Tkinter?
  4. Tools and Setup
    • Installing Python
    • Installing Tkinter
  5. Game Design for GUI-Based Tic Tac Toe
  6. Step-by-Step Implementation
    • Setting Up the GUI Window
    • Designing the Game Board
    • Handling Player Turns
    • Checking for Wins and Draws
    • Resetting the Game
  7. Full Python Code for GUI Tic Tac Toe
  8. Enhancing the Game
  9. Conclusion
  10. FAQs

1. Introduction

Python is recognized for its ease of use and adaptability, which makes it an excellent choice for developing both console and graphical user interface applications. Creating a GUI-based Tic Tac Toe game in Python not only makes the game visually attractive but also teaches you about event-driven programming and user interaction.


2. Why Build a GUI-Based Tic Tac Toe Game in Python?

Building a GUI for Tic Tac Toe has several advantages:

  • It introduces you to Tkinter, Python’s standard GUI library.
  • The graphical interface makes the game more interactive and enjoyable.
  • You’ll gain experience in structuring event-driven applications.

3. What is Tkinter?

Tkinter serves as Python’s integrated library for developing graphical user interfaces. It provides a range of widgets like buttons, labels, and text boxes, making it easy to design interactive applications.


4. Tools and Setup

Installing Python

Make sure Python is installed on your system. Download it from the official Python website.

Installing Tkinter

Tkinter comes pre-installed with Python on most systems. To verify, run the following command:

				
					python -m tkinter

				
			

If a blank window opens, Tkinter is already installed. If not, install it using:

				
					sudo apt-get install python3-tk  # For Linux users

				
			

Setting Up Your Editor

Use any Python editor you prefer, such as Visual Studio Code, PyCharm, or IDLE.


5. Game Design for GUI-Based Tic Tac Toe

The game will feature the following components:

  1. A grid of buttons arranged in a 3×3 format, representing the game board.
  2. Player turns and symbols (X or O).
  3. Win/draw detection logic displayed as messages.
  4. A Reset button to start a new game.

6. Step-by-Step Implementation

Step 1: Setting Up the GUI Window

First, we’ll create the main game window using Tkinter.

				
					import tkinter as tk
from tkinter import messagebox

# Initialize the main window
root = tk.Tk()
root.title("Tic Tac Toe")

				
			

Step 2: Designing the Game Board

We’ll create a 3×3 grid of buttons to serve as the game board.

				
					# Initialize game variables
board = [" " for _ in range(9)]
current_player = "X"

# Function to update the board
def button_click(button, index):
    global current_player
    if board[index] == " ":
        board[index] = current_player
        button.config(text=current_player)
        if check_win(current_player):
            messagebox.showinfo("Game Over", f"Player {current_player} wins!")
            reset_game()
        elif check_draw():
            messagebox.showinfo("Game Over", "It's a draw!")
            reset_game()
        else:
            current_player = "O" if current_player == "X" else "X"

# Create buttons
buttons = []
for i in range(9):
    btn = tk.Button(root, text=" ", font=("Arial", 24), height=2, width=5, 
                    command=lambda i=i: button_click(buttons[i], i))
    buttons.append(btn)
    btn.grid(row=i // 3, column=i % 3)

				
			

Step 3: Handling Player Turns

Switching turns is essential for the game to work. The button_click function handles this automatically.

Step 4: Checking for Wins and Draws

We’ll define functions to check for a win or a draw after each move.

				
					def check_win(player):
    win_combinations = [
        [0, 1, 2], [3, 4, 5], [6, 7, 8],  # rows
        [0, 3, 6], [1, 4, 7], [2, 5, 8],  # columns
        [0, 4, 8], [2, 4, 6]              # diagonals
    ]
    for combo in win_combinations:
        if all(board[i] == player for i in combo):
            return True
    return False

def check_draw():
    return all(spot != " " for spot in board)

				
			

Step 5: Resetting the Game

To let players start a new game, we’ll add a reset feature.

				
					def reset_game():
    global board, current_player
    board = [" " for _ in range(9)]
    current_player = "X"
    for button in buttons:
        button.config(text=" ")

				
			

Add a reset button to the GUI:

				
					reset_btn = tk.Button(root, text="Reset", font=("Arial", 18), command=reset_game)
reset_btn.grid(row=3, column=0, columnspan=3)

				
			

7. Full Python Code for GUI Tic Tac Toe

Here’s the complete code for the GUI-based Tic Tac Toe game in Python:

				
					import tkinter as tk
from tkinter import messagebox

# Initialize the main window
root = tk.Tk()
root.title("Tic Tac Toe")

# Initialize game variables
board = [" " for _ in range(9)]
current_player = "X"

# Functions
def button_click(button, index):
    global current_player
    if board[index] == " ":
        board[index] = current_player
        button.config(text=current_player)
        if check_win(current_player):
            messagebox.showinfo("Game Over", f"Player {current_player} wins!")
            reset_game()
        elif check_draw():
            messagebox.showinfo("Game Over", "It's a draw!")
            reset_game()
        else:
            current_player = "O" if current_player == "X" else "X"

def check_win(player):
    win_combinations = [
        [0, 1, 2], [3, 4, 5], [6, 7, 8],
        [0, 3, 6], [1, 4, 7], [2, 5, 8],
        [0, 4, 8], [2, 4, 6]
    ]
    for combo in win_combinations:
        if all(board[i] == player for i in combo):
            return True
    return False

def check_draw():
    return all(spot != " " for spot in board)

def reset_game():
    global board, current_player
    board = [" " for _ in range(9)]
    current_player = "X"
    for button in buttons:
        button.config(text=" ")

# Create buttons
buttons = []
for i in range(9):
    btn = tk.Button(root, text=" ", font=("Arial", 24), height=2, width=5,
                    command=lambda i=i: button_click(buttons[i], i))
    buttons.append(btn)
    btn.grid(row=i // 3, column=i % 3)

# Reset button
reset_btn = tk.Button(root, text="Reset", font=("Arial", 18), command=reset_game)
reset_btn.grid(row=3, column=0, columnspan=3)

# Run the application
root.mainloop()

				
			

8. Enhancing the Game

Here are several suggestions to enhance the game experience:

  • Add a scoreboard to track wins and draws.
  • Include a single-player mode with an AI opponent.
  • Allow players to select their symbols (X or O).
  • Use different themes for the game board.

9. Conclusion

Creating a GUI-based Tic Tac Toe game in Python is a rewarding project that teaches you both game logic and GUI programming. With the help of Tkinter, you can design an engaging game while enhancing your programming skills.


10. FAQs

Q1. Can I use a library other than Tkinter for the GUI?
Yes, libraries like PyQt or Kivy can also create GUIs, but Tkinter is beginner-friendly.

Q2. Can I run this game on any system?
Yes, as long as Python and Tkinter are installed.

Q3. How do I add an AI opponent?
You can implement the Minimax algorithm for a challenging AI.

Q4. Can I customize the game’s appearance?
Absolutely! Tkinter allows you to customize fonts, colors, and button sizes.

Q5. Is this code beginner-friendly?
Yes, it’s straightforward and designed for those new to programming.


Please don’t forget to leave a review.

Explore more by joining me on Instagram/ Facebook

Watch the Tutorial on YouTube Tic Tac Toe Game in Python 

Share your love

Leave a Reply

Your email address will not be published. Required fields are marked *