button in frames in python
<p><a href="http://i.stack.imgur.com/urPO6.png" rel="nofollow"><img src="http://i.stack.imgur.com/urPO6.png" alt="enter image description here"></a>
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</p>

<pre><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()
</code></pre>

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

<p>Just replace:</p>

<pre><code>handler = lambda a=i, b=j: self.update(a, b)
</code></pre>

<p>By:</p>

<pre><code>handler = lambda: self.update(i, j)
</code></pre>

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

<p>You can change <code>button_create</code> like this:</p>

<pre><code>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)
</code></pre>

<p>And then adapt the call in <code>frame</code> as follow:</p>

<pre><code>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)
</code></pre>

<p>Also note that you ought to inherit <code>object</code> class to use new-style classes:</p>

<pre><code>class Design(object):
    ...
</code></pre>

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

<pre><code>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)
</code></pre>

<p>Or simply:</p>

<pre><code>symbol = "XO"[Design.count % 2]
</code></pre>

python button tkinter frames