anchor介绍
anchor的中文意思是锚点。顾名思义,anchor就是确定某个组件的锚点,将锚点固定在窗口的指定位置。
anchor的参数
anchor一共有9个参数
e、w、s、n分别是east(东)、west(西)、south(南)、north(北)的缩写
n | 北 |
ne | 东北 |
e | 东 |
se | 东南 |
s | 南 |
sw | 西南 |
w | 西 |
nw | 西北 |
center | 中间 |
下图表示这9个锚点参数锚定的位置
用一个例子更好理解
import tkinter as tk
root = tk.Tk()
root.geometry('400x500+500+400')#创建窗口
can_ = tk.Canvas(root,width=400,height=400,
bg='pink')
can_.pack(padx=100,pady=100,anchor='nw')#创建画布
root.mainloop()
(padx = 100,pady = 100,anchor = 'nw')是指将画布的左上角作为锚点,并将此锚点固定在距离窗口上边缘(pady)100像素,以及距离左边缘(padx)100像素的位置。
运行结果