35

a. Have placed a widget in the row 0 in the grid as shown below.

self.a_button = Button(root, text="A Button")
self.a_button.grid(row=0, column=1)

b. And tried placing another widget in row 2 inside the grid.

self.b_button = Button(root, text="B Button")
self.b_button.grid(row=2, column=1)

But when I run the program, I don't see any space between the widgets, rather its stacked once after the other.

So, how do I program to allow space between two widgets placed in different rows? Share your comments !!

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Vimo
  • 1,061
  • 3
  • 12
  • 22

2 Answers2

62

When you pack the widget you can use

self.a_button = Button(root, text="A Button") 
self.a_button.grid(row=0, column=1, padx=10, pady=10)

Using padx and pady you can add padding to the outer side of the button and alternatively if you want to increase the size of the button you can add inner padding using ipadx and ipady.

If you want more on the Grid function you can view all the options and uses here.

Alex Peters
  • 2,601
  • 1
  • 22
  • 29
Dan Alexander
  • 2,004
  • 6
  • 24
  • 34
  • Thanks for the info. Works fine as required. Appreciate that. – Vimo Sep 18 '16 at 08:01
  • @radioxoma works with ttk for me, what are you trying to do? – Allan Lago Oct 23 '19 at 17:36
  • @Allan Lago `padx=10, pady=10` shouldn't work with ttk widgets – radioxoma Oct 24 '19 at 10:18
  • @radioxoma Just tried a simple example with treeview: my_Tree = ttk.Treeview(root, columns=('1', '2', '3')); my_Tree.grid(row=0, column=0, padx=100, pady=100); And this seems to work exactly as intended. Also tried searching up for a case in which padx/pady wouldn't work with ttk and couldn't find any. Is there anything specific that you are trying to do that you couldn't get it to work with? – Allan Lago Oct 25 '19 at 14:36
  • 1
    @radioxoma `padx=` and `pady=` are part of `grid()` geometry manager. It can be used with either `tk` or `ttk` widgets. Same applies to `padx=` and `pady=` for `pack()` geometry manager. – howdoicode Nov 09 '20 at 01:56
  • 2
    Dead link, I'm afraid. – Edward Falk May 14 '21 at 02:34
  • Dead link [revived](https://web.archive.org/web/20201112011230/http://effbot.org/tkinterbook/grid.htm). – Alex Peters May 23 '22 at 13:17
2

I think that you already got the answer, but I will share my solution to have space between two lines which works for me well.

spacer1 = tk.Label(win, text="")
spacer1.grid(row=4, column=0)

you can have this between to labels or entries as empty space at location row= 4, column= 0. You may want to modify the size of the space by adding pad sizes to spacer1.grid(row=4, column=0, padx= 10, pady= 10) or modify the label like spacer1 = tk.Label(win, text="", font=('Times New Roman, 40)) which ever works for you. The out put will be the space between two rows(3 & 5). I hope the solution helps you.

Ali Taheri
  • 116
  • 3