1
- import tkinter as tk #provides a library of basic elements of GUI widgets
2
1
from 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
5
4
def check_winner (board , player ):
6
5
# Check rows, columns, and diagonals for a win
7
6
for i in range (3 ):
@@ -63,7 +62,8 @@ def best_move(board):
63
62
def make_move (row , col ):
64
63
if board [row ][col ] == ' ' :
65
64
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' )
67
67
if check_winner (board , 'X' ):
68
68
messagebox .showinfo ("Tic-Tac-Toe" , "You win!" )
69
69
root .quit ()
@@ -79,15 +79,15 @@ def make_move(row, col):
79
79
def ai_move ():
80
80
row , col = best_move (board )
81
81
board [row ][col ] = 'O'
82
- buttons [row ][col ].config (text = 'O' )
82
+ buttons [row ][col ].configure (text = 'O' )
83
83
if check_winner (board , 'O' ):
84
84
messagebox .showinfo ("Tic-Tac-Toe" , "AI wins!" )
85
85
root .quit ()
86
86
elif is_board_full (board ):
87
87
messagebox .showinfo ("Tic-Tac-Toe" , "It's a draw!" )
88
88
root .quit ()
89
-
90
- root = tk . Tk ()
89
+ # change old UI code to customtkinter UI
90
+ root = ctk . CTk ()
91
91
root .title ("Tic-Tac-Toe" )
92
92
93
93
board = [[' ' for _ in range (3 )] for _ in range (3 )]
@@ -96,8 +96,8 @@ def ai_move():
96
96
for i in range (3 ):
97
97
row_buttons = []
98
98
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 )
101
101
row_buttons .append (button )
102
102
buttons .append (row_buttons )
103
103
0 commit comments