Python Tkinter - Geometry managers

Pybeginner
By -
0




The manager is about how we put widgets (example: button, label, etc.) into a window. Tkinter has three mechanisms for managing geometry: place, pack, and grid.

Themanager place uses absolute coordinates in pixels.

Themanager pack places the widgets on one of the four sides. New widgets are placed alongside existing widgets.

Themanager grid places the widgets in a grid similar to a work sheet resizing dynamic.

 

Python Tkinter - place

 

This geometry manager organizes widgets by placing them in a specific position on the parent widget.

Syntax

widget.place (place_options)

 

Here is the list of possible options -

anchor - The exact location of the widget to which other options refer: it can be N, E, S, W, NE, NW, SE or SW, directions of the compass indicating the corners and sides of the widget; the default is NW (the upper left corner of the widget)

bordermode - INSIDE (the default) to indicate that other options refer to the parent's interior (ignoring the parent's border); OUTSIDE otherwise.

heightwidth - Height and width in pixels.

relheight, relwidth - Height and width as a fluctuation between 0.0 and 1.0, as a fraction of the height and width of the parent widget.

relxrely - horizontal and vertical displacement as a fluctuation between 0.0 and 1.0, as a fraction of the height and width of the parent widget.

xy - Horizontal and vertical offset in pixels.

 

Example:
 

from tkinter import *

window = Tk()
window. title("Place geometry")
window.geometry('300x300')


label1 = Label(window, text="First Label", )
label1.place(height = 100, width = 100)

label2 = Label(window, text="Second Label")
label2.place(x = 100, y = 100)

window.mainloop()


In the example above, the first label was configured to occupy a height and width of 100 pixels, while the second label was configured to move a distance of 100 pixel horizontal and 100 pixel vertical
 



 

Python Tkinter - pack

 

This geometry manager organizes widgets into blocks before placing them in the parent widget.

Syntax

widget.pack(options_pack)

 

widget.pack can use the following keyword arguments:

expand, whether or not to fill in the space left by the relative widget.

fill, if you want to expand to fill the entire space (NONE (default), X, Y, or BOTH).

side,the side to which packaging (TOP (default), BOTTOM, LEFT, or RIGHT)

 

Example:

 

from tkinter import *

window = Tk()
window. title("Hello world")
window.geometry('300x300')


label1 = Label(window, text="First Label",fg = "red")
label1.pack (side = TOP)


label2 = Label(window, text="Second Label", fg = "green")
label2.pack (side = RIGHT)

label3 = Label(window, text="Third Label",fg = "blue")
label3.pack (side = LEFT)


label4 = Label(window, text="Fourth Label",fg ="black")
label4.pack (side = BOTTOM)

window.mainloop()

 



 

Python Tkinter - grid

 

This geometry manager organizes widgets in a table-like structure in the parent widget.

Syntax

widget.grid(grid_options)

 

The most commonly used keyword arguments in widget.grid are as follows:

row, the widget row (the lowest idle pattern);

rowpan, the number of columns that a widget spans (pattern 1);

column, the widget column (default 0);

columnpan, the number of columns that a widget spans (default 1);

ipadxipady - How many pixels the widget should contain, horizontally and vertically, within the widget's borders.

padxpady - How many pixels to place on the widget, horizontally and vertically, outside the edges of v.

sticky, where to place the widget if the grid cell is bigger than it (combination of N, NE, E, SE, S, SW, W, NW)

The rows (row) and columns (column) are zero indexed. The lines increase and the columns increase, going to the right.

 

Example:

 

from tkinter import *

window = Tk()
window. title("Grid")
window.geometry('300x300')

label1 = Label(window, text="First Label",fg = "red")
label1.grid(row = 0, column = 0)

label2 = Label(window, text="Second Label", fg = "green")
label2.grid(row = 0, column = 1)

label3 = Label(window, text="Third Label",fg = "blue")
label3.grid (row = 1, column = 0)

label4 = Label(window, text="Fourth Label",fg ="black")
label4.grid(row = 1, column = 1)

window.mainloop()




Post a Comment

0Comments

Post a Comment (0)