Why is Tkinter widget stored as None? (AttributeError: ‘NoneType’ object …)(TypeError: ‘NoneType’ object …) [duplicate]

widget is stored as None because geometry manager methods grid, pack, place return None, and thus they should be called on a separate line than the line that creates an instance of the widget as in:

widget = ...
widget.grid(..)

or:

widget = ...
widget.pack(..)

or:

widget = ...
widget.place(..)

And for the 2nd code snippet in the question specifically:

widget = tkinter.Button(...).pack(...)

should be separated to two lines as:

widget = tkinter.Button(...)
widget.pack(...)

Info: This answer is based on, if not for the most parts copied from, this answer.

Leave a Comment