Why is `None` returned instead of tkinter.Entry object?

后端 未结 3 2046
别跟我提以往
别跟我提以往 2020-12-20 23:51

I\'m new to python, poking around and I noticed this:

from tkinter import *
def test1():
    root = Tk()
    txtTest1 = Entry(root).place(x=10, y=10)
    pri         


        
相关标签:
3条回答
  • 2020-12-21 00:24

    You are creating an object (txtTest1) and then calling a method on that object (place). Because you code that as one expression, the result of the final method is what gets returned. place returns None, so txtTest1 gets set to None

    If you want to save a reference to a widget you need to separate the creation from the layout (which is a Good Thing To Do anyway...)

    txtTest1 = Entry(root)
    txtTest1.place(x=10, y=10)
    
    0 讨论(0)
  • 2020-12-21 00:28

    The place method of Entry doesn't return a value. It acts in-place on an existing Entry variable.

    0 讨论(0)
  • 2020-12-21 00:31

    because Entry.place() returns None

    in a more C-like language you could do:

    (txtTest1 = Entry(root)).place(x=10, y=10)
    

    and txtText1 would be the Entry object, but that syntax is illegal in Python.

    0 讨论(0)
提交回复
热议问题