Jay
Jay

Reputation: 13

tkinter .grid always top-left justified

Python 3.3 on Ubuntu.

I can't seem to get tkinter's .grid function to work correctly. I have the following code;

import sys
from tkinter import *

mwin = Tk()
mwin.title("Window")
mwin.geometry("450x450+500+100")

header = Label(text="The Label").grid(row=5,column=2)

mwin.mainloop()

It doesn't seem to matter what I put in for .grid(row=x,column=y), the label remains top left justified. What am I missing?

Upvotes: 1

Views: 2116

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 386342

By default, a row has zero height and a column has zero width. When you place something only at row 5 column 2, rows 0-4 are effectively invisible, as are columns 0 and 1. It's only when you put a widget in a cell, or when you use rowconfigure and/or columnconfigure that a row or column gets a non-zero size.

Upvotes: 1

FabienAndre
FabienAndre

Reputation: 4623

The grid geometry manager is relative. You lay out your widgets on a virtual grid, telling alignment and ordering through row and col. Widget sharing the same row will be vertically aligned, and widget with a row number higher than another will be below.Thus, you will notice differences only if you insert other widgets on your virtual grid.

Moreover, when you write header = Label(...).grid(...), you store in header the result of grid (which is always None). Use preferentially

header = Label(...)
header.grid(...)

Upvotes: 2

Related Questions