Here's my current GUI layout for my checkers game:
As you can see, it consists of a Menu along the top, a Canvas on the left where I draw the checkerboard, a toolbar (Frame) on the top right where I have various formatting/navigation buttons, and a Text widget that is used to annotate moves. Currently, I am using a grid layout for the widgets.
Here's what I need to do:
- Be able to show/hide a scrollbar in the Text widget when the amount of text grows larger than the widget size. (This seems to require the grid layout, according to this article.)
- Change the font and/or size of the text in the Text widget [via a Preferences dialog] and not leave weird gaps around the Text widget. (This seems to require a pack layout because the Text widget can only be given a width and height in characters not pixels … that means the Text widget grows or shrinks when I change the font or size, and the window won't adjust to fit with a grid layout. I've been trying to use Font.measure to adjust the Text widget size according to the font selected, but I still get gaps because I can't resize the widget down to the exact pixel.)
- My final solution needs to be cross-platform (both Windows & Linux, and hopefully Mac).
Which layout can I use to meet my requirements? If neither will work completely, which layout (grid or pack) will get me closest to my goal? Thanks!
Best Answer
For this simple layout you could use
grid
,pack
or both. Neither has a clear advantage in this particular case. Both have the resize behavior you desire.Off the top of my head I would use a horizontal frame to hold the buttons, and pack the buttons in it. I would then probably use grid to place the toolbar, text widget and scrollbar inside a frame. Pack can be used too, either would work. That takes care of the right side.
If you want the menubar the way it is in the picture (ie: non-standard, only over the chessboard) I would use a similar technique: another frame for the left side with the menubar packed on the top, chessboard on the bottom.
i would then use pack in the main window, with statusbar on the bottom, the chessboard on the left, and then the text area on the right.
However, it's better to use a standard menubar which means you don't need a containing frame for the chessboard/menubar combination
Here's a quick hack at one solution using a standard menubar. This uses the technique of putting most widgets as children of the parent, then using the
in_
parameter to put them in a container. This makes it much easier to change the layout later since you don't have to change a whole hierarchy, but only placement of widgets in containers.