this is my first time asking a question here, so please bear with me. I'm trying to write a tic-tac-toe game for two players that is supposed to notify you when a player has won and who won. The game worked well, but then I tried doing it with OOP and it stopped working. As you probably can see, I'm quite new to it and haven't grasped the concept completely yet. TkInter is also something I have never worked with before.
I tried putting the click function before and after the __init__ function but neither works.
from tkinter import *
from tkinter.font import Font
import tkinter.messagebox
turn = True
playerX = False #If True, Player X won
playerO = False #If True, Player O won
try:
while True:
class Game():
def click(self, button):
global turn
global playerX
global playerO
########## Test whether button is blank and then inserts 'X'
if(button["text"]=="" and turn == True):
button["text"]= "X"
if(button_1["text"]=="X" and button_2["text"]=="X" and button_3["text"]=="X" or
button_4["text"]=="X" and button_5["text"]=="X" and button_6["text"]=="X" or
button_7["text"]=="X" and button_8["text"]=="X" and button_9["text"]=="X" or
button_1["text"]=="X" and button_5["text"]=="X" and button_9["text"]=="X" or
button_3["text"]=="X" and button_5["text"]=="X" and button_7["text"]=="X" or
button_1["text"]=="X" and button_4["text"]=="X" and button_7["text"]=="X" or
button_2["text"]=="X" and button_5["text"]=="X" and button_8["text"]=="X" or
button_3["text"]=="X" and button_6["text"]=="X" and button_9["text"]=="X"):
tkinter.messagebox.showinfo(title="Congrats", message="Player X won!")
self.root.update()
playerX = True
exit()
self.root.title("Player O")
turn=False
########### Test whether button is blank and then inserts 'O'
elif(button["text"]=="" and turn == False):
button["text"] = "O"
if(button_1["text"]=="O" and button_2["text"]=="O" and button_3["text"]=="O" or
button_4["text"]=="O" and button_5["text"]=="O" and button_6["text"]=="O" or
button_7["text"]=="O" and button_8["text"]=="O" and button_9["text"]=="O" or
button_1["text"]=="O" and button_5["text"]=="O" and button_9["text"]=="O" or
button_3["text"]=="O" and button_5["text"]=="O" and button_7["text"]=="O" or
button_1["text"]=="O" and button_4["text"]=="O" and button_7["text"]=="O" or
button_2["text"]=="O" and button_5["text"]=="O" and button_8["text"]=="O" or
button_3["text"]=="O" and button_6["text"]=="O" and button_9["text"]=="O"):
tkinter.messagebox.showinfo(title="Congrats", message="Player O won!")
self.root.update()
playerO = True
exit()
self.root.title("Player X")
turn = True
def __init__(self):
self.root = Tk()
self.root.title("Tic-Tac-Toe")
self.root.resizable(width=False, height=False)
self.root.font = Font(family="Times 80", size=80)
button_1 = Button(self.root, text = "", height = 6, width = 12, command = lambda: self.click)
button_2 = Button(self.root, text = "", height = 6, width = 12, command = lambda: self.click)
button_3 = Button(self.root, text = "", height = 6, width = 12, command = lambda: self.click)
button_4 = Button(self.root, text = "", height = 6, width = 12, command = lambda: self.click)
button_5 = Button(self.root, text = "", height = 6, width = 12, command = lambda: self.click)
button_6 = Button(self.root, text = "", height = 6, width = 12, command = lambda: self.click)
button_7 = Button(self.root, text = "", height = 6, width = 12, command = lambda: self.click)
button_8 = Button(self.root, text = "", height = 6, width = 12, command = lambda: self.click)
button_9 = Button(self.root, text = "", height = 6, width = 12, command = lambda: self.click)
button_1.grid(row=0)
button_2.grid(row=0, column=1)
button_3.grid(row=0, column=2)
button_4.grid(row=1)
button_5.grid(row=1, column=1)
button_6.grid(row=1, column=2)
button_7.grid(row=2)
button_8.grid(row=2, column=1)
button_9.grid(row=2, column=2)
play = Game()
play.root.mainloop()
except:
tkinter.messagebox.showinfo(title="Error", message="Sorry there was an error!")