1- import tkinter as tk #provides a library of basic elements of GUI widgets
21from tkinter import messagebox #provides a different set of dialogues that are used to display message boxes
3- import random
4-
2+ import customtkinter as ctk
3+ import customtkinter as messagebox
54def check_winner (board , player ):
65 # Check rows, columns, and diagonals for a win
76 for i in range (3 ):
@@ -63,7 +62,8 @@ def best_move(board):
6362def make_move (row , col ):
6463 if board [row ][col ] == ' ' :
6564 board [row ][col ] = 'X'
66- buttons [row ][col ].config (text = 'X' )
65+ # in tk we use the config but in customtkinter we use configure for
66+ buttons [row ][col ].configure (text = 'X' )
6767 if check_winner (board , 'X' ):
6868 messagebox .showinfo ("Tic-Tac-Toe" , "You win!" )
6969 root .quit ()
@@ -79,15 +79,15 @@ def make_move(row, col):
7979def ai_move ():
8080 row , col = best_move (board )
8181 board [row ][col ] = 'O'
82- buttons [row ][col ].config (text = 'O' )
82+ buttons [row ][col ].configure (text = 'O' )
8383 if check_winner (board , 'O' ):
8484 messagebox .showinfo ("Tic-Tac-Toe" , "AI wins!" )
8585 root .quit ()
8686 elif is_board_full (board ):
8787 messagebox .showinfo ("Tic-Tac-Toe" , "It's a draw!" )
8888 root .quit ()
89-
90- root = tk . Tk ()
89+ # change old UI code to customtkinter UI
90+ root = ctk . CTk ()
9191root .title ("Tic-Tac-Toe" )
9292
9393board = [[' ' for _ in range (3 )] for _ in range (3 )]
@@ -96,8 +96,8 @@ def ai_move():
9696for i in range (3 ):
9797 row_buttons = []
9898 for j in range (3 ):
99- button = tk . Button (root , text = ' ' , font = ('normal' , 30 ), width = 5 , height = 2 , command = lambda row = i , col = j : make_move (row , col ))
100- button .grid (row = i , column = j )
99+ button = ctk . CTkButton (root , text = ' ' , font = ('normal' , 30 ), width = 100 , height = 100 , command = lambda row = i , col = j : make_move (row , col ))
100+ button .grid (row = i , column = j , padx = 2 , pady = 2 )
101101 row_buttons .append (button )
102102 buttons .append (row_buttons )
103103
0 commit comments