Difference between writing Layout management in one line and two line in tkinter

67 Views Asked by At

Can someone explain me the difference between writing layout management in this format i.e, in two lines

e1 = Entry(root, textvariable=x)
e1.grid(row=1, column=2)

and writing layout management in this format i.e., in one line

e1 = Entry(root, textvariable=x).grid(row=1, column=2)

Why can't I use get() when I write layout management in single line.

1

There are 1 best solutions below

0
On

In python, when you do x=y().z(), x is set to the result of .z().

pack, grid, and place all are designed to return None. Thus, e1 = Entry(root, textvariable=x).grid(row=1, column=2) return None So e1 is None. The value None doesn’t have any methods which is why you get the error AttributeError: 'NoneType' object has no attribute 'get'

Further, in my experience, it is always better to separate widget creation from widget layout. Trying to put it all on one line makes the code harder to understand and harder to maintain. By separating the code and grouping layout commands together it makes it much easier to visualize the layout just by looking at the code.