Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add space between two widgets placed in grid in tkinter ~ python?

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 !!

like image 526
Vimo Avatar asked Sep 18 '16 07:09

Vimo


People also ask

How do you add a space between two buttons in Python?

We can use the margin-right property for the first button element to create a space between buttons. As a result, a margin of a certain width to the button's right will be created. Then, the other button will reside next to the margin.

What is add separator in Tkinter?

Tkinter supports a variety of widgets to make GUI more and more attractive and functional. The Separator widget is used to partition the tkinter widgets such as label, buttons etc. Using this widget we can make our design more attractive and intuitive.

Can you use place and grid together in Tkinter?

Important: pack(), place(), and grid() should not be combined in the same master window. Instead choose one and stick with it.

What is padding in Tkinter?

Padding enhances the layout of the widgets in an application. While developing an application in Tkinter, you can set the padding in two or more ways. The geometry manager in Tkinter allows you to define padding (padx and pady) for every widget (label, text, button, etc).


2 Answers

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.

like image 84
Dan Alexander Avatar answered Oct 13 '22 08:10

Dan Alexander


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.

like image 2
Ali Taheri Avatar answered Oct 13 '22 07:10

Ali Taheri