Grid inside the frame?

Is it possible to place a grid of buttons in Tkinter inside another frame?

I want to create a game like tic-tac-toe and I want to use the grid function to put gamesquares (these will be the buttons). However, I would like to have other things in a GUI other than the playing field, so it is not ideal to just have everything in one grid.

To illustrate:

O | X | X | ---------- | O | O | X | Player 2 wins! ---------- | X | O | X | 

The tic tac toe board is in the grid consisting of all the buttons, and the β€œ2 player winner” is the label inside the frame.

This is a simplification of what I'm trying to do while carrying with me, since I have developed the program so far (the board is dynamically created), the grid makes the most sense.

Edit: Was there a thought, but when I start it, nothing happens? If I select a frame bit, this will happen. 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() 
+7
python tkinter grid frame
source share
2 answers

Figured out a way to do this 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 a mesh inside a frame inside a frame is a bit of a hack to get a pad around the mesh, but it works, so I'm happy.

+7
source share

You can embed Tk widgets arbitrarily deeply. Enter the manual :

The size of any main widget is determined by the size of the "subordinate" widgets "inside. The packer is used to control where subordinate widgets are displayed inside the master into which they are packed. You can pack widgets into frames and frames into other frames, to achieve the layout you want. In addition, dynamic dynamics make additional changes to when it is packaged.

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

+1
source share

All Articles