I want the X and O animations to switch back and forth upon mouse clicks. The problem is in the function XorO. I don't really understand why, but it will only create Xs when I click it. I think it may have to do with how I wrote the turn variable. Here is what I have.
from tkinter import *
tk = Tk()
width = 600
third = width / 3
canvas = Canvas(width=width, height=width)
tk.title = ("Tic Tac Toe")
line1 = canvas.create_line(200, 0, 200, 600)
line2 = canvas.create_line(400, 0, 400, 600)
line3 = canvas.create_line(0, 200, 600, 200)
line4 = canvas.create_line(0, 400, 600, 400)
def mouse_click(event):
col = int(event.x / third)
row = int(event.y / third)
XorO(row, col)
def XorO(row,col):
class XsorOs:
turn = 1
if turn is (1 or 3 or 5 or 7 or 9):
canvas.create_line(col * third, row * third, (col + 1) * third, (row + 1) * third)
canvas.create_line((col + 1) * third, row * third, col * third, (row + 1) * third)
else:
canvas.create_oval(col * third + 5, row * third + 5, (col + 1) * third - 5, (row + 1) * third - 5)
turn += 1
canvas.pack()
canvas.bind("<Button-1>", mouse_click)
canvas.mainloop()
See Question&Answers more detail:os