Search code examples
pythontkintertkinter-layout

placing labels within tkinter frame using grid


I'm trying to place labels within a grid within a Frame. However, they seem to be positioned at the wrong location. In the following code, I'm trying to place Labels at row=1,column=0 and row=2,column=0. However, they get placed at row=0,column=0 and row=1,column=0. Please see the code below. How do I get tkinkter to place the labels at the location I desire?

import tkinter

root = tkinter.Tk()
root.wm_title('testing grid layout')

# creates frames 
# height and width are specified in inches but do not seem to be respected
Frame00 = tkinter.Frame(root,bg = 'white', width = '1.0i', height='5.0i')

# divide the root grid into one row and one column
root.grid_rowconfigure(0, weight=0)
root.grid_columnconfigure(0, weight=0)

# place Frame00 into the root grid
Frame00.grid(column=0,row=0)

# Frame00 has four rows and one column
Frame00.grid_rowconfigure(0,weight=0)
Frame00.grid_rowconfigure(1,weight=0)
Frame00.grid_rowconfigure(2,weight=0)
Frame00.grid_rowconfigure(3,weight=0)
Frame00.grid_columnconfigure(0,weight=0)

# I'm specifying row=1 and column=0
# but it seems to be placed in row=0 and column=0
label1 = tkinter.Label(Frame00, text='Label 1')
label1.grid(row=1,column=0)

# I'm specifying row=2 and column=0
# but it seems to be placed in row=1 and column=0 
label2 = tkinter.Label(Frame00, text='Label 2')
label2.grid(row=2,column=0)

tkinter.mainloop()


Solution

  • Row 0 is empty so it takes up no space. You can give it some content such as a Label with a fixed height. Also your grid_rowconfigure() and grid_columnconfigure() calls seem to indicate that you might need to research that topic. I believe the weights are 0 by default so you could pick the ones you want to have expand into extra space and give them weight=1 and ignore the rest.