pack, grid and place are all used to manage the layout of all components under the same parent, where:
pack arranges components in the order they are added grid arranges components in rows/columns place allows the programmer to specify the size and position of components
When should I use the grid manager?
The grid manager is arguably the most flexible of Tkinter's three layout managers. If you only want to learn to use one layout manager, grid is definitely the one to use. gird is especially handy when you're designing dialog boxes. If you've been using pack to construct window layouts, you'll wish you hadn't learned grid earlier. It's easy to use a grid to do what you've been building with many frames and packs.
Note: Don't mix pack and grid in the same parent component, because Tkinter is seriously trying to figure out which layout manager to use first. ...... So much so that you can wait half an hour and Tkinter is still struggling with the result!
usage
To arrange components using grid, just tell it where you want to place them (row/column, row option for rows, cloumn option for columns). In addition, you don't have to indicate the size of the grid (the location where the grid distributes the components is called the grid) in advance, because the manager calculates it automatically.
import tkinter as tk root = () # column The default value is 0 (root, text="Username").grid(row=0) (root, text="Password.").grid(row=1) (root).grid(row=0, column=1) (root, show="*").grid(row=1, column=1) ()
By default components are centered in the corresponding grid, you can modify this feature by using the sticky option. The values that can be used for this option are "e", "w", "s", "n" (ewsn stands for east, west, south, north, north, south, left, west, right, east) and combinations of them. Therefore, we can make the Label left-aligned by sticky = "w":
(root, text="Username").grid(row=0, sticky="w") (root, text="Password.").grid(row=1, sticky="w")
Sometimes you may need to use several grids to place a component, can you do that? Of course you can, you just need toSpecifying rowspan and columnspan allows you to span rows and columns:
import tkinter as tk root = () # column The default value is 0 (root, text="Username").grid(row=0, sticky="w") (root, text="Password.").grid(row=1, sticky="w") (root).grid(row=0, column=1) (root, show="*").grid(row=1, column=1) photo = (file="") (root, image=photo).grid(row=0, column=2, rowspan=2, padx=5, pady=5) #rowspan=2 across two lines (text="Submit", width=10).grid(row=2, columnspan=3, pady=5) #columnspan=3 across three columns ()
methodologies
Note: All methods below apply to all components.
grid(**options)
-- The table below details the specific meaning and usage of each option:
options (as in computer software settings) | hidden meaning |
column | 1. Specify the column in which the component is to be inserted (0 for column 1) 2. The default value is 0 |
columnspan | Specify how many columns (across columns) to display the component in |
in_ | 1. Place the component in the component specified by this option 2. The specified component must be the parent component of the component |
ipadx | Specify horizontal inside margins |
ipady | Specify inside margins in the vertical direction |
padx | Specify the outer margin in the horizontal direction |
pady | Specify the outer margin in the vertical direction |
row | Specifies the line in which the component is inserted (0 for line 1) |
rowspan | Specify how many lines (across lines) to display the component on |
sticky | 1. control the position of the component in the space allocated by the grid 2. "n", "e", "s", "w" and combinations thereof can be used to locate (ewsn stands for east-west, north-south, up-north, down-south, left-west, right-east). 3. Use the plus sign (+) to indicate elongated fill, e.g. "n" + "s" means to elongate the component vertically to fill the grid, "n" + "s" + "w" + "e" means to fill the whole grid. 4. center the display if the value is not specified |
grid_bbox(column=None, row=None, col2=None, row2=None)
-- Returns a 4-tuple description of the bounding rectangle where the component is located -- If the column and cow parameters are specified, returns the bounding rectangle description of the component at that location (column, cow)
-- if 4 arguments are specified, returns a qualified rectangular description of all components from (column, cow) to (col2, row2)
-- e.g. grid_bbox(0, 0, 1, 1) returns the bounding rectangle where the 4 components are.
grid_columnconfigure(index, **options)
-- Setting column properties
-- Note: This sets the columns of the grid owned by the component.
-- The options that can be set and their meanings are as follows:
options (as in computer software settings) | hidden meaning |
minsize | Specify the minimum width of the column |
pad | Specifies the horizontal margins of the largest grid in the column. |
weight | 1. Specify the relative distance between columns 2. The default value is 0 3. This is more difficult for you to understand, small fish or a detailed explanation: the first time you create a window, grid will automatically assign the size of the window according to the size of the component, when you stretch the size of the window will have a blank display. This option is to specify whether the columns and columns are filled with blanks, the default is not filled. In addition, the value of this option specifies how many times the whitespace is filled, for example, a column with weight = 2 will be filled twice as much as a column with weight = 1. For example, a column with weight = 2 will be filled twice as much as a column with weight = 1. So if you want an even fill, just set weight = 1 for all columns. |
grid_configure(**options)
-- Same as grid()
grid_forget()
-- "Remove" the component from the screen
-- doesn't destroy the component, just doesn't see it anymore
-- Components that have been "deleted" can be displayed through a grid or other layout manager, but the options settings for the grid they are on will not be restored.
grid_info()
-- Returns the options for the current grid as a dictionary.
grid_location(x, y)
-- Returns the grid position at (or near) the given coordinates (x, y).
-- The return value is a 2-tuple representation of the grid (columns, rows).
grid_propagate(flag)
-- If turned on, the parent component is automatically resized to accommodate all child components
-- default is on (flag = True)
-- This method only applies to the parent component
grid_remove()
-- Same as grid_forget(), but restores remembering the option settings of the grid the component is on
grid_rowconfigure(index, **options)
-- Setting of line attributes
-- Note: This sets the rows of the grid owned by the component.
-- The options that can be set and their meanings are as follows:
options (as in computer software settings) | hidden meaning |
minsize | Specify the minimum height of the line |
pad | Specifies the vertical margins of the largest grid in the column. |
weight | 1. Specify relative distance between rows 2. The default value is 0 3. This is more difficult for you to understand, if you don't understand it, you can refer to the detailed explanation of grid_columnconfigure() above. |
grid_size()
-- Returns the size of the grid owned by the component.
-- The return value is a 2-tuple representing the number of grids (columns, rows) respectively
grid_slaves(row=None, column=None)
-- Returns all subcomponents of the component as a list
-- This method only applies to the parent component
To this article on the use of Python Tkinter layout management grid is introduced to this article, more related Python Tkinter grid content please search my previous posts or continue to browse the following related articles I hope you will support me in the future!