How to add space between two widgets placed in grid in tkinter ~ python?
Question:
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 !!
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.
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.
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 !!
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.
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.