Default padding for Tkinter grid

Discussion in 'Python' started by Amr, May 4, 2009.

  1. Amr

    Amr Guest

    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
     
    Amr, May 4, 2009
    #1
    1. Advertising

  2. En Mon, 04 May 2009 10:27:49 -0300, Amr <> 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
     
    Gabriel Genellina, May 4, 2009
    #2
    1. Advertising

  3. Amr

    Amr Guest

    On May 4, 3:57 pm, "Gabriel Genellina" <> wrote:
    > En Mon, 04 May 2009 10:27:49 -0300, Amr <> 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.

    --Amr
     
    Amr, May 4, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Girish
    Replies:
    4
    Views:
    691
    Patrick.O.Ige
    Jul 19, 2005
  2. =?Utf-8?B?QXNzYW5LaGFuIElzbWFpbA==?=

    How to transfer a value from an Invisible Grid to an visible Grid

    =?Utf-8?B?QXNzYW5LaGFuIElzbWFpbA==?=, Oct 13, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    477
    =?Utf-8?B?QXNzYW5LaGFuIElzbWFpbA==?=
    Oct 17, 2005
  3. Danny Tuppeny

    Grouped grid? Or grid within datalist?

    Danny Tuppeny, Oct 27, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    513
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Oct 27, 2005
  4. Marc
    Replies:
    0
    Views:
    459
  5. UKuser

    Default padding

    UKuser, Feb 22, 2007, in forum: HTML
    Replies:
    2
    Views:
    579
    Andy Dingley
    Feb 22, 2007
Loading...

Share This Page