Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

How might I utilize the grid() to put a gadget at the lower part of a window? For instance, this code will put these labels directly under one another, yet imagine a scenario where I need to move label_4 to the lower part of the window, or nearer to the bottom, how could this be accomplished.

label_1 = Label(text = 'Name 1').grid(row = 0, sticky = W)

label_2 = Label(text = 'Name 2').grid(row = 1, sticky = W)

label_3 = Label(text = 'Name 3').grid(row = 2, sticky = W)

label_4 = Label(text = 'Name 4').grid(row = 3, sticky = W)

1 Answer

0 votes
by (26.4k points)
edited by

In the event that you need to utilize the grid system for this, you need to make a "spacer" cell in the grid system (a vacant one), which can progressively get the spaces left from the window, and pushes the last label to the base.

from Tkinter import *  # or tkinter if you use Python3

root = Tk()

label_1 = Label(master=root, text='Name 1')

label_2 = Label(master=root, text='Name 2')

label_3 = Label(master=root, text='Name 3')

label_4 = Label(master=root, text='Name 4')

label_1.grid(row=0)

label_2.grid(row=1)

label_3.grid(row=2)  # this is the 2nd

label_4.grid(row=4)  # this is the 4th

root.rowconfigure(index=3, weight=1)  # add weight to the 3rd!

root.mainloop()

But, for this scenario, I recommend you to use pack instead of using grid

from Tkinter import *  # or tkinter if you use Python3

root = Tk()

label_1 = Label(master=root, text='Name 1')

label_2 = Label(master=root, text='Name 2')

label_3 = Label(master=root, text='Name 3')

label_4 = Label(master=root, text='Name 4')

label_1.pack()

label_2.pack()

label_3.pack()

label_4.pack(side=BOTTOM)

root.mainloop()

Join the python online course fast, to learn python concepts in detail and get certified.

Related questions

Browse Categories

...