Tkinter (46) layout geometry Manager


Layout geometry managers are divided into three categories

  • Pack the parts according to the orientation and roughly
  • Grid parts are placed in the form of tables and divided into grids
  • Place the parts according to the coordinate position and place them accurately

Note: the same container, pack and grid can not be mixed, which will cause tclerror exception, place can be mixed

Pack geometry Manager

  1. The parts are packed into the container in sequence. The first part is placed against the edge, and the inner part is defined as the upper edge. After placing, the size of the container is calculated. The most commonly used options are as follows
option explain Default value Available values
after After the encapsulated part
anchor Positioning mode CENTER NW/N/NE/E/SE/S/SW/W/CENTER
before In front of the encapsulated part
expand Whether to extend the part when the window size changes 0 YES/NO, True/False, 1/0
fill The direction in which the dimension of the part can be extended after the part is put into the container NONE X/Y/BOTH/NONE
ipadx Internal separation 0 positive integer
ipady Internal separation 0 positive integer
padx External interval 0 positive integer
pady External interval 0 positive integer
side After the parts are put into the container, which direction do they lean towards TOP LEFT/TOP/RIGHT/BOTTOM
in_ Parent container part
  1. Example description
example explain
pack() Minimum occupied space
pack(side=LEFT, fill=Y) Put it to the left, and the Y direction extends to the maximum position
pack(side=TOP, fill=X) Put it on the top and extend it to the maximum position in the X direction
pack(side=RIGHT, fill=NONE) Put it to the right, no extension
pack(side=TOP, fill=BOTH) Put it on top of it, and extend to the maximum position in X and Y directions
pack(expand = 1) When the container size changes, the size of the part remains unchanged, but the container extends
pack(fill=X, expand=1) When it is placed above, it extends in X direction. When the container size changes, the part also extends in X direction
  1. Pack correlation method
method explain
pack_configure() Same as pack ()
pack_forget() Hide the part, but its original pack option will be lost, and pack () will display the part again
pack_info() Returns a dictionary of the pack options and their values for the part
pack_propagate(flag) Whether the size of the part will be determined by the child part, which is set to true internally. If there is no parameter, the set value is returned
pack_slaves() Returns a list of subassemblies of an assembly in packaging order

Grid geometry Manager

  1. A 2D table is divided into two parameters, row in the vertical direction and column in the horizontal direction. The two parameters form a grid. Each grid can only be put into one part, and a part can also be placed into multiple squares. The height / degree of the grid is determined by the highest / lowest part of the row / column
option explain

Each grid is represented by a rowcolumnAnd columnrowIt starts from 0

The number of cells is determined by the number of rowscolumnspanAnd number of columnsrowspan(2,2) represents that the part occupies 4 grids

Horizontal and vertical spacing of components

Horizontal and vertical separation of the external parts
sticky The degree of each cell is determined by the maximum height of the column, and the height is determined by the maximum height of the row

So the grid is probably larger than the part, so the position of the component in the grid is determined bystickydecision

1. Set in the centerCENTER

2. In the middle of the edge,N(I),S(b),E(right),W(left)

3. Lean on the corner,NE code > (upper right corner),NW(upper left corner),SE(lower right corner),SW(lower left corner)

4. Lengthening,NS(vertical),EW(level),NSEW(full extension)

5. Others can beN, E, SandW(plus+Together)
in_ Set new parent assemblyin_The assembly must be a child of the parent assembly when the assembly was created
  1. Grid related method
method explain
w.grid_anchor(anchor=None) If no row / column has weight, set how to locate the grid and set it as NW
w.grid_bbox(column=None, row=None, col2=None, row2=None) latticecloumnreachcol2(including),Row code > torow2Coordinate frame occupied by (including)_ x,>(left_ x, top_ y, width, height)
w.grid_columnconfigure(N, option=value, …) Set the column n option as shown in 3
w.grid_configure Same as grid ()
w.grid_forget() Hidden part, but its originalgridAll options will be lost,grid()The component is displayed again
w.grid_info() partsgridA dictionary of options and their values
w.grid_location(x, y) The lattice (col, row) of the position of punctuation (x, y)
w.grid_propagate() The size of the extension part. The built-in value is 1. 0 does not extend
w.grid_remove() Hidden part, but its originalgridThe options remain,grid()The component is displayed again
w.grid_rowconfigure(N, option=value, …) Set the row n option as shown in 3
w.grid_size() Returns the number of horizontal and vertical grids of a part (columnspan, rowspan)
w.grid_slaves(row=None, column=None) Returns the list of components in a row, column. None means no part
  1. When the window size changes, the size change of the part is determined by the table setting of the parent part
method explain
w.columnconfigure(N, option=value, …) Set column n option
w.rowconfigure(N, option=value, …) Set row n option

The options are as follows

option explain
minsize The minimum size of a dot element will not be displayed when there is no component
pad Provides extra spacing for the largest grid
weight Extend the grid for more space, in quarter size

Note: to be able to extend a component when the window size changes, you must be in the parent component tocolumnconfigureandrowconfigureset upweightOption, which must also be in thegridMethodstickyIt can be extended

Place geometry Manager

  1. The place geometry manager is the least used one, but it is the most accurate one, relying on the coordinate system
option explain
anchor Component orientation
bordermode Whether to consider “inside” or “outside” of the parent part
height Component height element
width Component dimension
relwidth The ratio of component dimension to parent component is 0.0 ~ 1.0
relheight 0.0.0 ~ parent component ratio
x Component location x coordinate
y Y coordinate of component location
relx Component positioning X coordinate, the ratio of relative parent component is 0.0 ~ 1.0, 1.0 right end
rely The ratio of the component to the parent component is 0.0 ~ 1.0, and the lower end of 1.0
in_ Parent container part
  1. Place correlation method
method explain
place_configure() Same as place ()
place_forget () Hide the part, but its original place option will be lost, and place () will display the part again
place_info() Returns the dictionary of the part place option and its values
place_slaves() Returns a list of subassemblies of an assembly in packaging order

To be continued

This work adoptsCC agreementThe author and the link to this article must be indicated in the reprint

Jason Yang

Recommended Today

[Q & A share the second bullet] MySQL search engine, after watching the tyrannical interviewer!

Hello, I’m younger brother. A few days ago, I shared the second interview question, the interview site of search engine in MySQL. This question is the interview at normal temperature. After reading it, I’m sure you will gain something in terms of database engine If you haven’t read my first share, you can refer to […]