How to use grid_forget to eliminate a specific ordering of buttons after they have been instantiated
Question: I am building the GUI for a boardgame for my software engineering class. I am
using the TKinter toolkit on Python 2.7 (windows). I am stuck right now
because I cant seem to find a way to ignore/forget a certain ordering of
buttons. Essentially, I am trying to create a grid of buttons that would
represent my game board. And right now, I have a game board that has a total
of 49 buttons on a 7x7 grid.

So far this is what I have been able to do:

  1. Instantiate all my button objects where columns = x and rows = y. This easily build a grid of x*y
  2. I then place each button into a list (lets call this list1)
  3. I want to use my list of button objects to ignore/forget/delete (for lack of a better description) certain buttons. I am thinking I can create a 2nd list (list2) of the indexes of the button objects that I want to use grid_forget on and then compare my two lists and only keep the ones that are not in list2. Unfortunately, this doesnt work out the way I want it to. Here is the code:
    
          gameboard = ttk.Labelframe(root, padding = (8,8,8,8), text = "Gameboard", 
                  relief = "sunken")
      #forgetButtons will not be displayed on the game board b/c they do not have a  
      #label (they are not a: room, hallway, starting space)
      forgetButtons = [0,1,3,5,6,7,13,14,16,18,21,30,32,41,42,43,45,46,47,48]
      #this list tracks all the buttons on the gameboard
      myButtons=[]
      count = 0
          for x in range(7): #build a 7x7 grid of buttons (49 buttons total)
              for y in range(7):
                  btn = Button(gameboard, width=7, height=4)
                  myButtons.append(btn)
                  btn.grid(column=x, row=y, padx = 3, pady = 3)
    
                  #do some comparison here between the two lists 
                  #to weed out the buttons found in forgetButtons
    
                  #**or maybe it should not be done here?**
    
                  btn.config(text="Room%d\none\ntwo\nfour\nfive" % x)
    


Answer: You don't need `grid_forget` these widgets if you just don't create them.

    
    
    import itertools
    import Tkinter as tk
    
    root = tk.Tk()
    forgetButtons = [0,1,3,5,6,7,13,14,16,18,21,30,32,41,42,43,45,46,47,48]
    myButtons = []
    
    for x, y in itertools.product(range(7), repeat=2):
        if not x*7 + y in forgetButtons:
            btn = tk.Button(root, width=7, height=4, text="Room%d\none\ntwo\nfour\nfive" % x)
            btn.grid(column=x, row=y, padx=3, pady=3)
            myButtons.append(btn)
    
    root.mainloop()
    

I don't know the order to calculate the position for `forgetButtons` (usually
the first index represents the row and the second one the column), but you can
easily switch it.

