Layout Manager (pack, grid, and place)

When you create graphical interfaces, the widgets in the window must have a way to be arranged relative to each other. Tkinter there are three types of layout managers -- pack, place, and grid. Each manager uses a different method to help us arrange widgets.

Pack Layout Manager

Pack() method turns each individual widget into a block. Each widget has its own size and the pack manager fits them all together just like you would do with real blocks. The manager stacks widgets on top of each other vertically like blocks. Of course, you can also achieve a horizontal layout by changing the side parameter to 'left' or 'right'. You can also change the height, width, and locations of the widgets. 

Some of the pack manager's more useful parameters are listed below:

side -- specifies the general location of the widget in the window, arguments are 'top', 'bottom', 'left', 'right' (default is 'top').
fill -- which directions you want the widget to fill in the parent window, can choose 'x', 'y' directions, or 'both'. 
padx, pady -- the number of pixels surrounding the widget to create a padding between other widgets, for horizontal or vertical padding.
ipadx, ipady -- how many pixels to use for padding inside the widget, also for horizontal or vertical padding
expand -- set to True if you want the widget to stretch if the parent window expands. Default is False. 
anchor -- where the widget is placed in the parent widget, specified by  'n', 's', 'e', 'w', or some combination of them. Default is 'center'.


Grid Layout Manager

The grid method organizes widgets in a 2-dimensional table-like structure (think of an Excel spreadsheet), where each cell can hold a widget.  Placing widgets in the window is as simple as specifying the row and column values. The top left corner has values row=0 and column=0, and increase by 1 as you move right or down in the grid.

Grid Parameters

Now let's look at some of the main parameters that can help you arrange widgets using the grid layout manager.

row, column -- the row and column values for the location of the widget. Both start from 0.

columnspan, rowspan -- specifies how many columns or rows a widget will be in. This is very useful to help get the spacing right for your widgets.
padx, pady -- the number of pixels surrounding the widget to create padding between other widgets, for horizontal or vertical padding.
ipadx, ipady -- how many pixels to use for padding inside the widget, also for horizontal or vertical padding.
sticky -- specifies a value of S, N, E, W, or a combination of them, e.g. NW, NE, SW, or SE. The parameter tells which side of the "cell" the widget will "stick" to . If you use W+E+N+S, then the widget will fill up the "cell". Default is to center the widget within the "cell".


Place Layout Manager

Place, allows you to organize widgets by specifying both the position and size of each widget relative to other widgets.

This method allows for a bit more control in designing your UI than pack or grid, if you are willing to play around with the various x and y values.

Place Parameters:
in_ -- specifies the master window for the widget.
x, y -- specifies the specific x and y values of the widget in the           
           parent  window.
relx, rely -- horizontal and vertical offset relative to the size of the                 parent widget, values between 0.0 and 0.1.
relwidth, relheight -- sets height and width of widget relative to the             size of the parent widget, values between 0.0 and 0.1.
anchor -- where the widget is placed in the parent widget, specified          by 'n', 's', 'e', 'w', or some combination of them. Default is 'center'.


If You Want to Know More About Layout Manager Plz visit Below Link

Post a Comment

Previous Post Next Post