سؤال

Could someone please explain why when you have an plain widget as one line Code A works

Entry(root, width=10).pack(side=LEFT,anchor=W)

but when you name it or attach a command to it, Code A no longer works and gives you Error Message B

self.my_entry = Entry(root, width=10).pack(side=LEFT,anchor=W)

and you must pack using a seperate line?

self.my_entry = Entry(root, width=10)
self.my_entry.pack(side=LEFT,anchor=W)

Code A

self.my_entry.get()

Error Message B

AttributeError: 'NoneType' object has no attribute 'get'
هل كانت مفيدة؟

المحلول

The pack method returns None. So

self.my_label = Label(root, text="My Label").pack(side=LEFT,anchor=W)

sets self.my_label to None. That is why further commands using self.my_label no longer work.

You've found the solution; call pack on a separate line:

self.my_label = Label(root, text="My Label")
self.my_label.pack(side=LEFT,anchor=W)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top