Is it possible to place a grid of buttons in Tkinter inside another frame?
I'm wanting to create a tic-tac-toe like game and want to use the grid feature to put gamesquares (that will be buttons). However, I'd like to have other stuff in the GUI other than just the game board so it's not ideal to just have everything in the one grid.
To illustrate:
O | X | X |
---------- |
O | O | X | Player 2 wins!
---------- |
X | O | X |
The tic tac toe board is in a grid that is made up of all buttons and the 'player 2 wins' is a label inside a frame.
This is an oversimplification of what I'm trying to do so bear with me, for the way I've designed the program so far (the board is dynamically created) a grid makes the most sense.
Edit: Had a thought but when I run it, nothing happens? If I take out the frame bit it does. Any ideas?
from Tkinter import *
root = Tk()
b = Button(root, text = "1")
b.grid(row=1, column=3)
b2 = Button(root, text = "2")
b2.grid(row=1, column=4)
f = Frame(root, bg = "red")
f.pack(side=RIGHT)
root.mainloop()
You can nest Tk widgets arbitrarily deep. Quoth the manual:
Indeed, a frame containing a (frame of buttons) and a label is how you must structure the layout you describe.
Figured out a way to do it finally:
Having the grid inside a frame inside a frame is a bit of a hack to get the padding around the grid working but it works so I'm happy.