I sit, I make tic tac toe, and then there was a problem - I had to change the text of the buttons from other functions, I couldn’t change it, because checking for text gives an error:

Exception in Tkinter callback Traceback (most recent call last): File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__ return self.func(*args) File "C:/Users/Чебупелька/Desktop/Python/tictactoe.py", line 16, in <lambda> c11 = Button(root, text="", command=lambda: c11l(), width=20, height=10) File "C:/Users/Чебупелька/Desktop/Python/tictactoe.py", line 74, in c11l if c11['text'] == '': NameError: global name 'c11' is not defined 

Code below, help than you can.

 from Tkinter import * import tkMessageBox i = 0 def start(): root = Tk() root.geometry("600x600") c00 = Button(root, text="", command=lambda: c00l(), width=20, height=10) c00.grid(row=0, column=0) c01 = Button(root, text="", command=lambda: c01l(), width=20, height=10) c01.grid(row=0, column=1) c02 = Button(root, text="", command=lambda: c02l(), width=20, height=10) c02.grid(row=0, column=2) c10 = Button(root, text="", command=lambda: c10l(), width=20, height=10) c10.grid(row=1, column=0) c11 = Button(root, text="", command=lambda: c11l(), width=20, height=10) c11.grid(row=1, column=1) c12 = Button(root, text="", command=lambda: c12l(), width=20, height=10) c12.grid(row=1, column=2) c20 = Button(root, text="", command=lambda: c20l(), width=20, height=10) c20.grid(row=2, column=0) c21 = Button(root, text="", command=lambda: c21l(), width=20, height=10) c21.grid(row=2, column=1) c22 = Button(root, text="", command=lambda: c22l(), width=20, height=10) c22.grid(row=2, column=2) def rst(): root.destroy() dstr = Button(root, text="Exit", command=lambda: rst(), width=5, height=5) dstr.grid(row=3, column=0) root.mainloop() def c00l(): global i, c00 if c00['text'] == '': c00['text'] = 'X' else: tkMessageBox.showinfo(title="ALARM!!!", message="ENGAGED!") 

    2 answers 2

    Something like this:

     import Tkinter as tk import tkMessageBox class TicTacToe: def __init__(self): self.root = tk.Tk() self.root.geometry("600x600") self.__create_buttons() self.__add_button_exit() def mainloop(self): self.root.mainloop() def exit(self): self.root.destroy() def __create_functions_for_buntton(self, row, column): def click(): if self.buttons[(row, column)]['text'] == '': self.buttons[(row, column)]['text'] = 'X' else: tkMessageBox.showinfo(title="ALARM!!!", message="ENGAGED!") return click def __add_button_exit(self): self.button_exit = Button(self.root, text="Exit", command=self.exit, width=5, height=5) self.button_exit.grid(row=3, column=0) def __create_buttons(self): self.buttons = {} for row in range(3): for column in range(3): self.buttons[(row, column)] = Tk.Button(self.root, text='', command=self.__create_functions_for_buntton(row, column), width=20, height=10) self.buttons[(row, column)].grid(row=row, column=column) #Using tictactoe = TicTacToe() tictactoe.mainloop() 

      make c11 (and all others) global or pass it to your function