I don't understand the game or what it is supposed to do, but you call clearbotones from c (button press). Shouldn't the color be set in c instead of creating new buttons each time? Hint, you use boton[] list. Add a print statement to clearbotones to print each time the new buttons are created and the bg color of the button, so you can see what is happening. Also you can create the buttons in a loop

       for j in range(abs(posH), abs(posH)+7):
            texto = str(matriz[i][j])

            btn = tk.Button(mainFrame,text="",bg="white",width=10,

an example using a 3X3 grid which just changes the background color of the clicked button. This employs a dictionary, but you could use a list just as easily. And a class avoids all of the messy globals

from Tkinter import *
from functools import partial

class ButtonTest:
   def __init__(self):
      self.top = Tk()
      self.button_dic = {}     ## pointer to buttons and StringVar()
      self.top.title('Buttons TicTacToe Test')
      self.top_frame = Frame(self.top, width =500, height=500)

      self.top_frame.grid(row=0, column=1, sticky="WE")
      for ctr in range(0, 3):   ## configure all columns to the same size
         self.top_frame.grid_columnconfigure(ctr, minsize=50)
         self.top_frame.grid_rowconfigure(ctr, minsize=37)

      exit = Button(self.top_frame, text='Exit', \
             command=self.top.quit).grid(row=10,column=0, columnspan=5)


   def buttons(self):
      """ create 9 buttons, a 3x3 grid
      for j in range(1, 10):
         b_row, b_col = divmod(j-1, 3)
         b = Button(self.top_frame, textvariable=sv, \
                    command=partial(self.cb_handler, j), bg='white')
         b.grid(row=b_row, column=b_col, sticky="WENS")
         self.button_dic[j]=b ## button number-->Tkinter ID

   def cb_handler(self, square_number):
       print square_number, "pressed"


Edited 2 Years Ago by woooee

This article has been dead for over six months. Start a new discussion instead.