Tic Tac Toe game in Python using Tkinter for a graphical user interface (GUI)
In this article, print_board function is used to display the Tic-Tac-Toe board with beautiful formatting using Unicode characters. The players can enter their moves by specifying a position from 1 to 9. The game checks for wins and ties and switches players after each turn.
Code
import tkinter as tk
from tkinter import messagebox
class TicTacToeGUI:
def __init__(self):
self.current_player = "X"
self.board = [" " for _ in range(9)]
self.create_gui()
def create_gui(self):
self.root = tk.Tk()
self.root.title("Tic-Tac-Toe")
self.buttons = []
for i in range(3):
row = []
for j in range(3):
button = tk.Button(self.root, text=" ", font=("Helvetica", 20, "bold"), width=6, height=3,
command=lambda i=i, j=j: self.make_move(i, j))
button.grid(row=i, column=j, padx=5, pady=5)
row.append(button)
self.buttons.append(row)
def make_move(self, row, col):
if self.board[row * 3 + col] == " ":
self.board[row * 3 + col] = self.current_player
self.buttons[row][col].config(text=self.current_player)
self.buttons[row][col].config(state=tk.DISABLED)
if self.check_win():
messagebox.showinfo("Game Over", "Player {} wins!".format(self.current_player))
self.reset_game()
elif " " not in self.board:
messagebox.showinfo("Game Over", "It's a tie!")
self.reset_game()
else:
self.current_player = "O" if self.current_player == "X" else "X"
def check_win(self):
win_patterns = [[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 pattern in win_patterns:
if self.board[pattern[0]] == self.board[pattern[1]] == self.board[pattern[2]] != " ":
return True
return False
def reset_game(self):
self.current_player = "X"
self.board = [" " for _ in range(9)]
for i in range(3):
for j in range(3):
self.buttons[i][j].config(text=" ", state=tk.NORMAL)
def start_game(self):
self.root.mainloop()
if __name__ == "__main__":
game = TicTacToeGUI()
game.start_game()
This code creates a GUI for the Tic-Tac-Toe game using the Tkinter library. The game window consists of 9 buttons arranged in a 3×3 grid. Each button represents a cell on the Tic-Tac-Toe board.
Output

Working Video
When a player clicks on a button, the make_move function is called to update the game state and display the current player’s symbol on the button. The game checks for a win or a tie after each move, and if either condition is met, a message box is displayed with the game result. Players can then reset the game and start a new round.
- To run the code, ensure that you have Tkinter installed (it comes pre-installed with most Python distributions).
- Then, execute the Python script and a window with the Tic-Tac-Toe game will appear.
- You can take turns making moves by clicking on the buttons, and the game will display the winner or declare a tie when appropriate.
Note: This code represents a simple implementation of the game and does not include advanced features such as computer AI for playing against the computer.