I'm writing a program in python,tkinter and I need the text on the button to update... is there a simple way to do this?

I suggest button.configure(text="new text") . Example:

from Tkinter import *

class App:
   def __init__(self, parent):
    self.myParent = parent
    self.fm = Frame(parent, width=400, height=300)
    self.fm.pack_propagate(0) # to keep the frame size
    self.fm.pack()
    
    self.index=0
    self.btnBlue = Button(self.fm, text = "Blue %d" % self.index, command=self.make_blue)
    self.btnBlue.pack()

   def make_blue(self):
    self.fm.configure(bg="blue")
    self.index += 1
    self.btnBlue.configure(text = "Blue %d" % self.index)

root = Tk()
root.title ("Color Option")
app = App(root)
root.mainloop()

well I did try both of those and neither worked (I might not be be getting StringVar() but oh well) but here is a sample of my code (I did clip lots out so don't complain about size please)

from Tkinter import *
board = {}
for x in range(1,10):
    board["%d"%x]=[0]
    for y in range(1,10):
        board["%d"%x].append(" ")
line1 = [6,3,2,7,8,1,9,4,5]
line2 = [4,8,7,5,9,6,2,1,3]
line3 = [5,1,9,2,4,3,8,7,6]
line4 = [8,6,4,3,5,2,7,9,1]
line5 = [7,5,1,9,6,8,3,2,4]
line6 = [2,9,3,1,7,4,6,5,8]
line7 = [9,4,5,6,3,7,1,8,2]
line8 = [1,7,6,8,2,5,4,3,9]
line9 = [3,2,8,4,1,9,5,6,7]
board['7'][7],board['7'][8],board['7'][9],board['8'][7],board['8'][8],board['8'][9],board['9'][7],board['9'][8],board['9'][9] = line1
board['7'][4],board['7'][5],board['7'][6],board['8'][4],board['8'][5],board['8'][6],board['9'][4],board['9'][5],board['9'][6] = line2
board['7'][1],board['7'][2],board['7'][3],board['8'][1],board['8'][2],board['8'][3],board['9'][1],board['9'][2],board['9'][3] = line3
board['4'][7],board['4'][8],board['4'][9],board['5'][7],board['5'][8],board['5'][9],board['6'][7],board['6'][8],board['6'][9] = line4
board['4'][4],board['4'][5],board['4'][6],board['5'][4],board['5'][5],board['5'][6],board['6'][4],board['6'][5],board['6'][6] = line5
board['4'][1],board['4'][2],board['4'][3],board['5'][1],board['5'][2],board['5'][3],board['6'][1],board['6'][2],board['6'][3] = line6
board['1'][7],board['1'][8],board['1'][9],board['2'][7],board['2'][8],board['2'][9],board['3'][7],board['3'][8],board['3'][9] = line7
board['1'][4],board['1'][5],board['1'][6],board['2'][4],board['2'][5],board['2'][6],board['3'][4],board['3'][5],board['3'][6] = line8
board['1'][1],board['1'][2],board['1'][3],board['2'][1],board['2'][2],board['2'][3],board['3'][1],board['3'][2],board['3'][3] = line9

def change(board,space,number):
    x,y = space
    board[x][y] = number    

while True:
    class App:

        def __init__(self, master):

            frame = Frame(master)
            frame.pack()

            button77 = Button(frame, text=board['7'][7],command=lambda:change(board,['7',7],number["number"]))
            button77.grid(row=1, column=1)

            button78 = Button(frame, text=board['7'][8],command=lambda:change(board,['7',8],number["number"]))
            button78.grid(row=1, column=2)

            button79 = Button(frame, text=board['7'][9],command=lambda:change(board,['7',9],number["number"]))
            button79.grid(row=1, column=3)

            button74 = Button(frame, text=board['7'][4],command=lambda:change(board,['7',4],number["number"]))
            button74.grid(row=2, column=1)

            button75 = Button(frame, text=board['7'][5],command=lambda:change(board,['7',5],number["number"]))
            button75.grid(row=2, column=2)

            button76 = Button(frame, text=board['7'][6],command=lambda:change(board,['7',6],number["number"]))
            button76.grid(row=2, column=3)

            button71 = Button(frame, text=board['7'][1],command=lambda:change(board,['7',1],number["number"]))
            button71.grid(row=3, column=1)

            button72 = Button(frame, text=board['7'][2],command=lambda:change(board,['7',2],number["number"]))
            button72.grid(row=3, column=2)

            button73 = Button(frame, text=board['7'][3],command=lambda:change(board,['7',3],number["number"]))
            button73.grid(row=3, column=3)

            number = {}
            number["number"] = 1

    root = Tk()

    app = App(root)

    root.mainloop()

when you click the "X" it does change the board

Take a look at the "buttons" function in this code, as it should help with the StringVar, and with creating several buttons using a for loop. In this case the buttons are stored in a dictionary.

from Tkinter import *
from functools import partial

class TicTacToe:
   def __init__(self, top):
      self.top = top
      self.button_dic = {}     ## pointer to buttons and StringVar()
      self.X_O_dict = {"X":[], "O":[]}  ## list of "X" and "O" moves
      self.top.title('Buttons TicTacToe Test')
      self.top_frame = Frame(self.top, width =500, height=500)
      self.buttons()
      self.top_frame.grid(row=0, column=1)

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

      self.player = True   ## True = "X", False = "O"

   ##-------------------------------------------------------------------         
   def buttons(self):
      """ create 9 buttons, a 3x3 grid
      """
      b_row=1
      b_col=0
      for j in range(1, 10):
         ## point the button number to a Tkinter StringVar()
         self.button_dic[j] = StringVar()
         ## set the StringVar() to the button number
         self.button_dic[j].set(str(j))
         ## set textvariable to the StringVar()
         b = Button(self.top_frame, textvariable = self.button_dic[j], \
                    command=partial(self.cb_handler, j))
         b.grid(row=b_row, column=b_col)

         b_col += 1
         if b_col > 2:
            b_col = 0
            b_row += 1

   ##----------------------------------------------------------------
   def cb_handler( self, square_number ):
      print "cb_handler", square_number
      if self.player:                       ## True = "X", False = "O"
         player = "X"
      else:
         player = "O"

      ##--- square not already occupied
      if square_number not in self.X_O_dict["X"] and \
         square_number not in self.X_O_dict["O"]:

         ## change the StringVar() to "X" or "O"
         self.button_dic[square_number].set(player)
         self.X_O_dict[player].append(square_number)
         print player, 
         self.check_for_winner(self.X_O_dict[player])
         self.player = not self.player
      else:
         print "Occupied, pick another"

   ##----------------------------------------------------------------
   def check_for_winner( self, list_in ):
      set_in = set(list_in)

      winner_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9], \
                     [1, 4, 7], [2, 5, 8], [3, 6, 9], \
                     [1, 5, 9], [3, 5, 7]]
      for winner in winner_list:
         if set(winner).issubset(set_in):
            print 'Is A Winner'
            self.top.quit()


##===================================================================
root = Tk()
BT=TicTacToe(root)
root.mainloop()
print BT.X_O_dict

And you should be able to replace button71-79 with a standard for loop (this code is not tested).

button71 = Button(frame, text=board['7'][1],command=lambda:change(board,['7',1],number["number"]))
button71.grid(row=3, column=1)
 
button72 = Button(frame, text=board['7'][2],command=lambda:change(board,['7',2],number["number"]))
button72.grid(row=3, column=2)
 
button73 = Button(frame, text=board['7'][3],command=lambda:change(board,['7',3],number["number"]))
button73.grid(row=3, column=3)

button74 = Button(frame, text=board['7'][4],command=lambda:change(board,['7',4],number["number"]))
button74.grid(row=2, column=1)
 
button75 = Button(frame, text=board['7'][5],command=lambda:change(board,['7',5],number["number"]))
button75.grid(row=2, column=2)
 
button76 = Button(frame, text=board['7'][6],command=lambda:change(board,['7',6],number["number"]))
button76.grid(row=2, column=3)

button77 = Button(frame, text=board['7'][7],command=lambda:change(board,['7',7],number["number"]))
button77.grid(row=1, column=1)
 
button78 = Button(frame, text=board['7'][8],command=lambda:change(board,['7',8],number["number"]))
button78.grid(row=1, column=2)
 
button79 = Button(frame, text=board['7'][9],command=lambda:change(board,['7',9],number["number"]))
button79.grid(row=1, column=3)

##------------------------------------------------------------------------
##  replace above with a for() loop, storing the buttons in a dictionary
##------------------------------------------------------------------------
button_dict = {}
button_row = 3
button_num = 1
for row_num in range(3):
    for col_num in range(3):
        this_button = 70+button_num
        button_dict[this_button] = Button(frame, text=board['7'][button_num], \
                                  command=lambda:change(board,['7',button_num], \
                                  number["number"]))
        button_dict[this_button].grid(row=button_row, column=col_num+1)
        button_num += 1

    button_row -= 1

ok well I figured out what I was doing wrong with IntVar() so Thanks to Woooee and Gribouillis for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.