Python 为什么返回'None'而不是tkinter.Entry对象?
我是python新手,四处闲逛,我注意到:Python 为什么返回'None'而不是tkinter.Entry对象?,python,tkinter,python-3.2,Python,Tkinter,Python 3.2,我是python新手,四处闲逛,我注意到: from tkinter import * def test1(): root = Tk() txtTest1 = Entry(root).place(x=10, y=10) print(locals()) def test2(): root = Tk() txtTest2 = Entry(root) txtTest2.place(x=10, y=10)#difference is this line
from tkinter import *
def test1():
root = Tk()
txtTest1 = Entry(root).place(x=10, y=10)
print(locals())
def test2():
root = Tk()
txtTest2 = Entry(root)
txtTest2.place(x=10, y=10)#difference is this line
print(locals())
test1()
test2()
产出包括:
'txtTest1':无
'txtTest2':
为什么test1有一个None
而不是条目的place
方法不返回值。它作用于现有的条目变量。因为Entry.place()不返回任何值
使用更类似C语言的语言,您可以执行以下操作:
(txtTest1 = Entry(root)).place(x=10, y=10)
txtText1将是Entry对象,但这种语法在Python中是非法的。您正在创建一个对象(txtTest1
),然后在该对象上调用一个方法(place
)。因为您将其编码为一个表达式,所以最终方法的结果就是返回的结果place
返回None
,因此txtest1
设置为None
如果要保存对小部件的引用,则需要将创建与布局分开(无论如何,这是一件好事…)
谢谢,我以为它会返回带有该位置的条目,但是你的回答很有意义。谢谢,我过去做过一些c++/c,所以这对我来说很有意义。
txtTest1 = Entry(root)
txtTest1.place(x=10, y=10)