Default padding for Tkinter grid

A

Amr

Hello all,

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.

Thanks,

--Amr
 
G

Gabriel Genellina

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)
 
A

Amr

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)

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.

--Amr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top