115

How can I add padding to a tkinter window, without tkinter centering the widget? I tried:

 self.canvas_l = Label(self.master, text="choose a color:", font="helvetica 12")
 self.canvas_l.grid(row=9, column=1, sticky=S, ipady=30)

and

 self.canvas_l = Label(self.master, text="choose a color:", font="helvetica 12")
 self.canvas_l.grid(row=9, column=1, rowspan=2, sticky=S, pady=30)

I want 30px padding only on the top of the label.

Jack S.
  • 2,501
  • 6
  • 25
  • 27

3 Answers3

282

The padding options padx and pady of the grid and pack methods can take a 2-tuple that represent the left/right and top/bottom padding.

Here's an example:

import tkinter as tk

class MyApp():
    def __init__(self):
        self.root = tk.Tk()
        l1 = tk.Label(self.root, text="Hello")
        l2 = tk.Label(self.root, text="World")
        l1.grid(row=0, column=0, padx=(100, 10))
        l2.grid(row=1, column=0, padx=(10, 100)) 

app = MyApp()
app.root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Interesting. Is this behavior described anywhere semi-official? My two favorite tk resources, [effbot](http://effbot.org/tkinterbook/grid.htm) and [New Mexico Tech](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/grid.html), don't give any hint that padding can be uneven in this way. Secret undocumented behavior? – Kevin Aug 25 '17 at 19:27
  • 1
    @Kevin: tkinter is just a pythonic wrapper around an embedded tcl interpreter which includes the tk library. The canonical docs for tk mention this feature. Here's the reference: http://tcl.tk/man/tcl8.5/TkCmd/grid.htm#M16 – Bryan Oakley Aug 25 '17 at 20:11
  • Works on Grid padx/pady, but not on Frame. Probably many more exceptions exists – Daniel Braun May 08 '18 at 16:26
  • 1
    @DanielBraun: it will work for frames, too. Tkinter doesn't care what type of widget it is. – Bryan Oakley May 08 '18 at 16:32
  • @BryanOakley actually `padx` and `pady` are standard tk widget options so we can use them in the dunder `init` method also. Using the 2 integer tuple form there will raise an exception `_tkinter.TclError: bad screen distance "3 7"`. While instead in a `grid` package manager invocation it's accepted. I've verified this behaviour on Python 3.7.X. – Giova Oct 31 '19 at 20:26
  • 2
    @Giova: yes, the tuple version only works for the `padx` and `pady` options of `grid` and `pack`. The `padx` and `pady` options of some widgets are different. You cannot use it for the `padx` and `pady` option of widgets. I'll try to clarify my answer. – Bryan Oakley Oct 31 '19 at 21:40
  • Is it possible to specify different left and right padx for the root window in tkinter?@BryanOakley – Milk_Shaykh Mar 30 '20 at 06:11
  • Just a note, `padx=` and `pady=` also accepts a 2-value list, such as `padx=[10, 20]`. Not just a 2-value tuple. In addition, the values can be either an integer or a string, such as `padx=["10", "20"]`, and they will still work. – howdoicode Nov 09 '20 at 01:42
8

There are multiple ways of doing that you can use either place or grid or even the packmethod.

Sample code:

from tkinter import *
root = Tk()

l = Label(root, text="hello" )
l.pack(padx=6, pady=4) # where padx and pady represent the x and y axis respectively
# well you can also use side=LEFT inside the pack method of the label widget.

To place a widget to on basis of columns and rows , use the grid method:

but = Button(root, text="hello" )
but.grid(row=0, column=1)
Anandakrishnan
  • 349
  • 5
  • 10
  • The variable/constant `LEFT` is `tkinter.LEFT`, available as just `LEFT` after `from tkinter import *` – drevicko Mar 31 '22 at 23:17
3
-pady {10,0}

this way you are mentioning padding on top as 10 and below as 0.

In python code, this might look like:

l = Label(root, text="hello" )
l.pack(pady=(10, 0)) 
drevicko
  • 14,382
  • 15
  • 75
  • 97
Vishwamithra
  • 190
  • 9
  • Merging this with @Anandakrishnan's answer, using eg `l.pack(pady=(0, 10))` works (not the use of a tuple). – drevicko Mar 31 '22 at 23:19