.pack_forget()无法正常工作 - python

我在使用.pack_forget()函数时遇到问题。如果我写:

a1= Button(root, text='button', width=10, command=buttonclick).pack()

函数a1.pack_forget()返回错误:

AttributeError: 'NoneType' object has no attribute 'pack_forget'

但是如果我写:

a1= Button(root, text='button', width=10, command=buttonclick)
a1.pack()

函数a1.pack_forget()正常工作。为什么较短的方法不起作用?

python大神给出的解决方案

在python中,当您执行x=foo().bar()时,会为x赋予bar()值。因此,对于ai=Button(...).pack(...)ai获得pack(...)的值。

对于tkinter,pack(...)grid(...)以及place(...)都返回None。因此,在您的代码中ai设置为None。这就是设计tkinter和python的方式。