1
votes

enter image description here Design of my game is something like this I need to develop Strategic tic tac toe(where in each block of tic tac toe we will find a tic tac toe)....so here i created 9 frames and each frame has 9 buttons... but when i click the button in any frame changes appear only in one frame ie; in (0,2) i know because its the last frame which is to be called..so i need help to rectify the problem...i tried but i did not get it thanks in advance Here's my code

from Tkinter import *

root = Tk()
root.title("Simple Design")
root.geometry("300x300")


class Design:
    count = 0

    def __init__(self):
        self.duplicates = {}
        self.block = {}
        self.button = {}
        for i in range(3):
            for j in range(3):
                self.duplicates[i, j] = "."
        self.frame()

    def frame(self):
        for i, j in self.duplicates:
            self.block[i, j] = Frame(root, background="blue")
            self.block[i, j].grid(row=i, column=j, ipadx=5, ipady=2)
            self.button_create(self.block[i, j])

    def button_create(self, frame):
        for i, j in self.duplicates:
            handler = lambda a=i, b=j: self.update(a, b)
            self.button[i, j] = Button(frame, command=handler, text=".", height=3, width=5)
            self.button[i, j].grid(row=i, column=j)

    def update(self, i, j):

        if (Design.count % 2 == 0):
            self.button[i, j]["text"] = "X"
            Design.count += 1
        else:
            self.button[i, j]["text"] = "O"
            Design.count += 1
        self.button[i, j]["state"] = "disabled"
        print (i, j)


d = Design()  # out of class
root.mainloop()
1

1 Answers

0
votes

There is a problem with your button handler: how can each button know its coordinates?

Just replace:

handler = lambda a=i, b=j: self.update(a, b)

By:

handler = lambda: self.update(i, j)

There is also a bug in frame method: You iterate duplicates and then call button_create which again iterates duplicates. So buttons are redefined in the inner loop.

You can change button_create like this:

def button_create(self, frame, i, j):
    handler = lambda: self.update(i, j)
    self.button[i, j] = Button(frame, command=handler, text=".", height=3, width=5)
    self.button[i, j].grid(row=i, column=j)

And then adapt the call in frame as follow:

def frame(self):
    for i, j in self.duplicates:
        self.block[i, j] = Frame(root, background="blue")
        self.block[i, j].grid(row=i, column=j, ipadx=5, ipady=2)
        self.button_create(self.block[i, j], i, j)

Also note that you ought to inherit object class to use new-style classes:

class Design(object):
    ...

Instead of a if ... else, you can use a mapping to calculate the symbol "X" or "O" in update:

def update(self, i, j):
    symbol = {0: "X", 1: "O"}[Design.count % 2]
    self.button[i, j]["text"] = symbol
    Design.count += 1
    self.button[i, j]["state"] = "disabled"
    print (i, j)

Or simply:

symbol = "XO"[Design.count % 2]