1

I am trying to make a tictac game, I created the buttons with for loop and saved them in dictionary as given code -

from tkinter import *
class TicTac():
    def __init__(self):
        self.window = Tk()
        self.window.title('Tic Tac Game')
        self.btns = {}
        self.buttons()
    def change_state(self,button):
        if button.cget('text') == '':
            button.configure(text = 'X')

    def buttons(self):
        for i in range(1,4):
            for j in range(1,4):
                button = self.btns['{x}{y}'.format(x=str(i),y=str(j))] = Button(self.window,text='',width=15,height=7,
                                                                                command=lambda: self.change_state(button))
                button.grid(row=i, column=j)

T = TicTac()

When I click on any button, the text of last button i.e 9 is changed and also in print statement it is printing .!button9 only not the button that was clicked, I want to change the text of button that was clicked.

Pankaj Sharma
  • 2,185
  • 2
  • 24
  • 50

0 Answers0