0

I am trying to get the content of "Entry" from Tkinter and use it as an argument for another script to execute. The function get() does not seem to work:

import Tkinter
master = Tkinter.Tk()

master.geometry('200x100')

def callback():
    #execfile("Hello.py")
    print e1.get()

L1 = Tkinter.Label(master, text="Files").grid(row=1)
L2 = Tkinter.Label(master, text="Dice score").grid(row=2)

e1 = Tkinter.Entry(master, bg="white").grid(row=1, column=1)
e2 = Tkinter.Entry(master, bg ="white").grid(row=2, column=1)

B = Tkinter.Button(master, text="Start script", command = callback).grid(row=0, column=0)

master.mainloop()

When I execute this script and press the button instead of printing what's in e1 entry I get:

print e1.get()

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

1 Answers1

2

The problem lies in this kind of chained call:

 e1 = Tkinter.Entry(master, bg="white").grid(row=1, column=1)

The grid method (as well as pack) does not return the Entry object. So, while the call to create the Entry succeeds and returns a new entry, and the call to .grid also succeeds, this later returns None, which is the object you keep in your L1, L2, e1, e2, B variables.

For all of them you should unfold the grid call in two lines - like this, else, you won't have a reference to the tkinter objects in Python:

e1 = Tkinter.Entry(master, bg="white"); e1.grid(row=1, column=1)
e2 = Tkinter.Entry(master, bg ="white"); e2.grid(row=2, column=1)

(You can check this if you try to print these variables with your code as is - and you will see the contain None)

jsbueno
  • 99,910
  • 10
  • 151
  • 209