Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
    print(locals())
def test2():
    root = Tk()
    txtTest2 = Entry(root)
    txtTest2.place(x=10, y=10)#difference is this line
    print(locals())
test1()
test2()

outputs contain:

'txtTest1': None

'txtTest2': <tkinter.Entry object at 0x00EADD70>

Why does test1 have a None instead of <tkinter.Entry object at ...?

I'm using python 3.2 and PyScripter.

like image 210
182764125216 Avatar asked Aug 03 '11 21:08

182764125216


People also ask

Why is tkinter entry Get function returning nothing?

You haven't given the entry widget any text before you call get so of course it returns an empty string.

How do I get entries in tkinter?

An Entry widget in Tkinter is nothing but an input widget that accepts single-line user input in a text field. To return the data entered in an Entry widget, we have to use the get() method. It returns the data of the entry widget which further can be printed on the console.

What is the difference between text and entry in tkinter?

The Entry widget is used to accept single-line text strings from a user. If you want to display multiple lines of text that can be edited, then you should use the Text widget. If you want to display one or more lines of text that cannot be modified by the user, then you should use the Label widget.

What does Tk () do in tkinter?

Tkinter is a Python package which comes with many functions and methods that can be used to create an application. In order to create a tkinter application, we generally create an instance of tkinter frame, i.e., Tk(). It helps to display the root window and manages all the other components of the tkinter application.


3 Answers

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

like image 117
jterrace Avatar answered Oct 06 '22 01:10

jterrace


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.

like image 33
jcomeau_ictx Avatar answered Oct 06 '22 01:10

jcomeau_ictx


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)
like image 35
Bryan Oakley Avatar answered Oct 06 '22 01:10

Bryan Oakley