0

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.

Raw Newton
  • 53
  • 9
  • It is because `Entry(...).grid(...)` returns the result of `grid(...)` which is `None`. For latest Python 3.8.x, you can use `(e1 := Entry(...)).grid(...)`. – acw1668 Oct 04 '20 at 02:27
  • See my answer on this question for details: https://stackoverflow.com/a/63079747/13629335 – Thingamabobs Oct 04 '20 at 06:50

1 Answers1

1

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685