views:

387

answers:

2

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()
+1  A: 

You can nest Tk widgets arbitrarily deep. Quoth the manual:

The size of any master widget is determined by the size of the “slave widgets” inside. The packer is used to control where slave widgets appear inside the master into which they are packed. You can pack widgets into frames, and frames into other frames, in order to achieve the kind of layout you desire. Additionally, the arrangement is dynamically adjusted to accommodate incremental changes to the configuration, once it is packed.

Indeed, a frame containing a (frame of buttons) and a label is how you must structure the layout you describe.

msw
Are you saying that what I'm asking isn't possible?I know I can put a frame within a frame but would it be possible to place a grid within a frame?
Sam
@Sam: you can't "place a grid within a frame" because a grid isn't something you can put anywere. The grid() method merely lays widgets out in a container. You aren't "adding a grid" you are merely "laying widgets in a grid pattern". And, as the answer says, you can nest Tk widgets arbitrarily deep (though in my many years of experience, you rarely need to go more than 3 or 4 deep).
Bryan Oakley
+1  A: 

Figured out a way to do it finally:

from Tkinter import * 

root = Tk()

f = Frame(root, bg = "orange", width = 500, height = 500)
f.pack(side=LEFT, expand = 1)

f3 = Frame(f, bg = "red", width = 500)
f3.pack(side=LEFT, expand = 1, pady = 50, padx = 50)

f2 = Frame(root, bg = "black", height=100, width = 100)
f2.pack(side=LEFT, fill = Y)

b = Button(f2, text = "test")
b.pack()

b = Button(f3, text = "1", bg = "red")
b.grid(row=1, column=3)
b2 = Button(f3, text = "2")
b2.grid(row=1, column=4)
b3 = Button(f3, text = "2")
b3.grid(row=2, column=0)

root.mainloop()

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.

Sam
The grid isn't a "thing" it is a method that widgets use to arrange themselves within a frame; note that there is no `Grid` constructed. This is the Tk model of composition, it is similar to that of TeX which itself was derived from 500 years of printing experience. Nested boxes is the method that we've found to arrange text-like objects: it isn't a hack.
msw