Display two frames of the same class with Python and Tkinter
Question: I wrote a simple game in Python. Now I'm trying to give it a GUI (currently it
is text based). Since there aren't moving objects, I decieded to use Tkinter
(I like the grid arrangement, the east coding to get a menu and the fact that
there is no need to install anything).

I have a few problems I can't seem to solve:

  1. I want two display two game boards on the screen. I created a frame class that contain the information and displays it, but it only shows the first one. The second is created but does not appear on screen.

  2. All the tiles on the board has the same functionality, but when I press a tile (label object) I want to use some metadata of the object. That is, I want to assign it a string and when I press it I want to get the string attached to the label I pressed. I can't figure out how to do it. **\--- Solved in the comments by Eric Levieil**

  3. (least important) Can I put two images, one on top of the other without using canvas/absolute position?

Here is my code:

    
    
    import tkinter as tk
    from tkinter import ttk
    
    class GameBoard(ttk.Frame):
        tiles = []
        def __init__(self, parent, temp, *args, **kwargs):
            ttk.Frame.__init__(self, parent)
            self.parent = parent
            self.graphics = {'1H':tk.PhotoImage(file='graphics/1H.gif'),
                    '0': tk.PhotoImage(file='graphics/0.gif')}
    
            for row_num in range(5):
                row = []
                for cell_num in range(10):
                    row.append(ttk.Label(self))
                    row[cell_num]['image'] = self.graphics[temp]
                    row[cell_num].bind('<Button-1>', lambda event: print('(%d,%d)'%(row_num,cell_num)))
                self.tiles.append(row)
    
            for row_num in range(5):
                for cell_num in range(10):
                    self.tiles[row_num][cell_num].grid(row=row_num, column=cell_num)
    
    
    class MainBoard(ttk.Frame):
        def __init__(self, parent, *args, **kwargs):
            ttk.Frame.__init__(self, parent)
            player_1_name = ttk.Label(self, text='Player 1').grid(row=0, sticky=tk.W)
            player_1_board = GameBoard(self, '0')
            player_1_board.grid(row=1, sticky=(tk.N, tk.W))       
            player_2_name = ttk.Label(self, text='Player 2').grid(row=2, sticky=tk.W)
            player_2_board = GameBoard(self, '1H')
            player_2_board.grid(row=3, sticky=(tk.S, tk.W))
            self.grid(row=0)
    
    
    
    def main():
        root = tk.Tk()
        root.title('Battleship')
        MainBoard(root)
        root.mainloop()
    
    if __name__ == '__main__':
        main()
    

Thanks, Uri

Edit: Here are the two images I'm using
[graphics.zip](http://www.math.tau.ac.il/~urigrupe/graphics.zip).


Answer: Managed it. Reinitialize your tiles in `GameBoard.__init__` and it will do.

Just before your first `for-Loop`:

`self.tiles=[]`

`for row_num in range(5):`

