Apr-25-2018, 04:11 PM
I would like to create an opportunity of restarting this game of Tic-Tac-Toe after it is finished. I was thinking of checking if all the created buttons have either "x" or "O" texts, and offering to restart then, but I could not find a way to do that.
Here is the code I would like to add to:
Here is the code I would like to add to:
import tkinter as tk
from functools import partial
class ButtonTest():
def __init__(self, master):
self.parent = master
self.buttons_list = []
for i in range(5):
for j in range(3):
button_num = i * 3 + j
button = tk.Button(self.parent, height=6, width=12)
button.grid(row=i, column=j)
button.bind("<Button-1>", partial(self.click, button_num))
self.buttons_list.append(button)
self.counter = []
def check_winner():
def click(self, button_num, event):
this_button = self.buttons_list[button_num]
the_actual_counter = len(self.counter)
if the_actual_counter % 2 == 0:
this_button["text"] = "X"
else:
this_button["text"] = "O"
self.counter.append("element")
master = tk.Tk()
BT = ButtonTest(master)
master.mainloop()
