3. tk geometry place
The place geometry manager allows you to arrange widgets within a window using absolute positions.
3.1. place
Place is not responsive to window size changes.
Place can use absolute positioning or relative positioning. Only absolute is considered below.
Absolute positioning uses coordinates of (0, 0) in the top left and increases to the right and down.
3.1.1. x and y coordinates of top left of widget
- widget.place(x=x_coord, y=y_coord)
- Use place() method to place a widget with its top left at the x and y coordinates.e.g. widget.place(x=50, y=75)The width and height of the widget are automatically determined by the content.
3.1.2. width and height of widget
- widget.place(x=x_coord, y=y_coord, width=widget_width, height=widget_height)
- Use place() method to place a widget with its top left at the x and y coordinates.Set the height and width of the widget: width=widget_width, height=widget_height.e.g. label2.place(x=50, y=75, width=60, height=80)
3.1.3. widget anchor
- widget.place(x=x_coord, y=y_coord, anchor='nw')
- The anchor parameter determines which part of the widget is positioned at the given coordinates.The default value of the anchor is 'nw' for the top left of the widget at the specified coordinates.parameter accepts values such as: 'n', 'ne', 'e', 'se', "s", 'sw', 'w', 'nw'These constants represent north, northeast, east, southeast, south, southwest, west, northwest.'center': This value instructs the place() method to position the center of the widget at the coordinates (x, y).
3.2. Sample code
import tkinter as tk
# window
root = tk.Tk()
root.title('Layout intro')
root.geometry('600x400')
# widgets
label1 = tk.Label(root, text = 'Label 1', background = 'red')
label2 = tk.Label(root, text = 'Label 2', background = 'pink')
label3 = tk.Label(root, text = 'Label 3', background = 'orange')
# place
label1.place(x=150, y=50)
label2.place(x=150, y=100, width=100, height=60)
label3.place(x=150, y=200, anchor="center")
# run
root.mainloop()