Default padding for Tkinter grid

Amr amrbekhit at
Mon May 4 17:10:38 CEST 2009

On May 4, 3:57 pm, "Gabriel Genellina" <gagsl-... at> wrote:
> En Mon, 04 May 2009 10:27:49 -0300, Amr <amrbek... at> escribió:
> > I've been spending the last few days experimenting with Tkinter. The
> > grid manager is nice and easy to use, but I have found that I am often
> > having to specify padx and pady options to every widget I add to my
> > grid. The way I am doing it is to create a dictionary:
> > paddding = {'padx': '1m', 'pady': '1m'}
> > and then apply it to the grid method using **padding.
> > However, I was wondering if there was a way of setting default padx
> > and pady controls for the grid, so that I can just call grid without
> > having to specify any extra parameters.
> You have to call grid() once on every widget, so adding **padding at the  
> end doesn't appear too bad to me:
>    label = Label(master, text="Hello world!")
>    widget.grid(row=3, col=1, **padding)
> What about a function:
> def grid_with_padding(widget, padx='1m', pady='1m', **kw):
>    widget.grid(padx=padx, pady=pady, **kw)
> If you want an uniform padding:
> def add_padding(container, padx='1m', pady='1m'):
>    nc, nr = container.grid_size()
>    for i in range(nc):
>      container.grid_columnconfigure(i, pad=padx)
>    for i in range(nr):
>      container.grid_rowconfigure(i, pad=pady)
> --
> Gabriel Genellina

Hi Gabriel,

Thanks for your reply - I like the method you mention at the end,
using grid_columnconfigure and grid_rowconfigure as I can use that to
apply setting to the whole grid at the end. I'll have a mess around
with it.


More information about the Python-list mailing list