Mar-10-2021, 01:38 PM
Hello, I have been creating a Tic Tac Toe game in Tkinter. It is supposed to draw X's and O's. A left click should place an X and a right click should place an O. There should also be a key on the keyboard that clears all of the Xs and Os on the board (your window should include a Label that explains how to clear the board). I am supposed to write the code using ONLY Label widgets for the spaces (you can use other Widgets for the grid lines). Since you are not using Button widgets, you will have to use event-binding to change what is in each space. You are NOT trying to keep up with wins and losses, and you should NOT worry about preventing a user from "cheating" (e.g., O taking over a space already claimed by X)
I have gotten the grid drawn and I have the first row code done but when I click in any of the blank spaces it doesn't draw anything. I know there is supposed to be more stringvars in order for this to work and this is the part where I am stuck and I am going to define the letter "C" to clear the board. It's supposed to work like this
How Tic Tac Toe supposed to work
I have gotten the grid drawn and I have the first row code done but when I click in any of the blank spaces it doesn't draw anything. I know there is supposed to be more stringvars in order for this to work and this is the part where I am stuck and I am going to define the letter "C" to clear the board. It's supposed to work like this
How Tic Tac Toe supposed to work
from tkinter import * from tkinter import ttk root = Tk() root.title("Tic Tac Toe") message = StringVar() # This is the portion that draws out the grid used in Tic Tac Toe label1 = ttk.Label(root, text="") label1.grid(row=1, column=0) label2 = ttk.Label(root, text="|") label2.grid(row=1, column=1) label3 = ttk.Label(root, text="") label3.grid(row=1, column=2) label4 = ttk.Label(root, text="|") label4.grid(row=1, column=3) label5 = ttk.Label(root, text="-") label5.grid(row=4, column=1) label6 = ttk.Label(root, text="-") label6.grid(row=4, column=2) label7 = ttk.Label(root, text="-") label7.grid(row=4, column=3) label8 = ttk.Label(root, text="-") label8.grid(row=4, column=4) label9 = ttk.Label(root, text=" ") label9.grid(row=5, column=0) label10 = ttk.Label(root, text="|") label10.grid(row=5, column=1) label11 = ttk.Label(root, text=" ") label11.grid(row=5, column=2) label12 = ttk.Label(root, text="|") label12.grid(row=5, column=3) label13 = ttk.Label(root, text="-") label13.grid(row=6, column=1) label14 = ttk.Label(root, text="-") label14.grid(row=6, column=2) label15 = ttk.Label(root, text="-") label15.grid(row=6, column=3) label16 = ttk.Label(root, text="-") label16.grid(row=6, column=4) label17 = ttk.Label(root, text=" ") label17.grid(row=7, column=0) label18 = ttk.Label(root, text="|") label18.grid(row=7, column=1) label19 = ttk.Label(root, text=" ") label19.grid(row=7, column=2) label20 = ttk.Label(root, text="|") label20.grid(row=7, column=3) #This is the code that draws out the O and X label21 = ttk.Label(root, text="") label21.grid(row=1, column=0) label21.bind("<Button-1>", lambda e: message.set("X")) label21.bind("<Button-3>", lambda e: message.set("O")) label22 = ttk.Label(root, text="") label22.grid(row=1, column=1) label22.bind("<Button-1>", lambda e: message.set("X")) label22.bind("<Button-3>", lambda e: message.set("O")) label22.grid(row=1, column=3) label22.bind("<Button-1>", lambda e: message.set("X")) label22.bind("<Button-3>", lambda e: message.set("O")) root.mainloop()