首页 > 解决方案 > 当您将变量设置为方法时,您将术语称为什么?

问题描述

from tkinter import *              
root = Tk()        
button1 = Button(root, text= "Hello world click here to close")   
y = button1.pack()         
print (type (y))      
root.mainloop()

当设置ybutton1.pack()时,您将这种技术称为什么?为什么它会返回“<class 'NoneType'>”?

标签: pythonmethodssyntaxnonetype

解决方案


y = button1.pack()设置ypack()方法的预期返回值。

例如,

foo = random.randint()设置为方法foo返回的随机数randint

foo = random.randint,但是,将randint方法分配给变量foo

由于此方法不返回任何内容,因此 y 没有值,并type()表示为

您要做的是使用y = button1.pack,它将将该功能分配给y

如果你type()y那之后继续运行,你会看到它返回它的类型为“函数”


推荐阅读