问题
Using pack has been described to me as like putting all of your widgets within an elastic band, the implications of this being that pack will try and keep your widgets in a small, neat area.
How close is this analogy to the truth, and what are it's inconsistencies?
回答1:
That analogy is not at all accurate. pack
doesn't try to keep things in a "small, neat area", and it really has no relationship at all to an elastic band.
pack
is conceptually very simple: it arranges widgets around the edges of the unallocated space in a parent widget. The easiest way to think of this is that you're filling a box from the outside and working yourself toward the middle. Using pack
, you are constantly filling an empty cavity, which is why this is referred to as the cavity model.
When you place a widget inside the cavity of a parent widget, you are placing it along one of the sides of the cavity. The widget then takes up that whole side of that cavity. For example, if you pack something with the option side="left"
, the widget will take up the whole left side of the cavity, and then reduce the size of the cavity for the next widget. If the parent is 1000 pixels wide and the child is 100 pixels wide, you will have then reduced the cavity down to 900 pixels wide for the next child widget.
The absolute best and most complete description of how pack
works can be found in the book Practical Programming in Tcl and Tk, by Brent Welch, Ken Jones, and Jeffrey Hobbs. A concise but complete description of the packer algorithm can be found in the tcl/tk pack man page. It is written from the perspective of a tcl/tk program (versus python/tkinter) but the fundamental aspects are the same no matter which language you use.
来源:https://stackoverflow.com/questions/51818550/tkinter-pack-layout-elastic-band-analogy