Python Tkinter Tic Tac Toe Game | #shorts

Posted by


In this tutorial, we will create a Tic Tac Toe game using Tkinter and Python. Tic Tac Toe is a classic game that is played on a 3×3 grid where two players take turns marking X and O on the grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game.

To get started, you will need to have Python installed on your computer. Tkinter comes pre-installed with Python, so you do not need to worry about installing it separately.

First, let’s import the necessary modules and create a window for our game:

import tkinter as tk

root = tk.Tk()
root.title("Tic Tac Toe")

Next, we will create a 3×3 grid of buttons to represent the Tic Tac Toe board. We will also define a function to handle the button click events:

def on_click(row, col):
    global player

    if board[row][col] == "" and not winner:
        board[row][col] = player
        buttons[row][col].config(text=player)

        check_winner()

        player = "X" if player == "O" else "O"

board = [["" for _ in range(3)] for _ in range(3)]
buttons = [[None for _ in range(3)] for _ in range(3)]

for i in range(3):
    for j in range(3):
        buttons[i][j] = tk.Button(root, text="", width=10, height=5,
                                  command=lambda i=i, j=j: on_click(i, j))
        buttons[i][j].grid(row=i, column=j)

In the on_click function, we first check if the button has not been clicked before and if there is no winner yet. If these conditions are met, we update the board with the current player’s mark, update the button text, and check for a winner.

Next, we will define a function to check for a winner after each move:

def check_winner():
    global winner

    for i in range(3):
        if board[i][0] == board[i][1] == board[i][2] != "":
            winner = board[i][0]
            break

        if board[0][i] == board[1][i] == board[2][i] != "":
            winner = board[0][i]
            break

    if board[0][0] == board[1][1] == board[2][2] != "":
        winner = board[0][0]
    elif board[0][2] == board[1][1] == board[2][0] != "":
        winner = board[0][2]

    if winner:
        tk.messagebox.showinfo("Game Over", f"{winner} wins!")

In the check_winner function, we check for horizontal, vertical, and diagonal matches to determine if there is a winner. If a winner is found, we display a message box with the winner’s name.

Finally, we will initialize the player variable and winner variable before starting the game:

player = "X"
winner = None

Now that we have completed the code for our Tic Tac Toe game, we can run the game by calling the mainloop method:

root.mainloop()

That’s it! You have successfully created a Tic Tac Toe game using Tkinter and Python. You can further enhance the game by adding features such as a reset button, keeping track of the score, or adding animations to the board. Have fun playing and customizing your Tic Tac Toe game!

0 0 votes
Article Rating
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@dsh124
1 month ago

bro intra project documentation kedaikuma

@Rathi413
1 month ago

Source code explanation please

@lmaooaooo336
1 month ago

Very good z im planning to learn tkinter library how ?

@trex7454
1 month ago

How sad…This Video Should Be Blown Up.