0

I receive a a blank output when using code:

from Tkinter import *

global entry1

root = Tk()

def printentry1():
    global entry1
    print entry1

entry1 = Entry(root)
button = Button(root,text="Submit",command=printentry1)

entry1.pack()
entry1=entry1.get()
button.pack()


root.mainloop()

I want it to print what they put in the entry box to Shell/Console. Its returning "". Please help running Py2.7

Jake L
  • 17
  • 3
  • You `get()` value only once (when redefine entry) so there's always a blank output. Why not just `print(entry1.get())` in your callback? – CommonSense May 23 '17 at 17:26
  • When i use print(entry1.get()) it prints a random thing such at .57381704L and it is different every time – Jake L May 23 '17 at 17:27

1 Answers1

2

Executing entry1=entry1.get() before mainloop will cause entry1 to have the value that is inside the Entry widget before the window appears. Since the user has had zero seconds to type anything in yet, that value will be the empty string. Delete that line.

Once you've deleted that line, printing entry in the callback will give you a value like ".57381704L". That is the widget's unique identifier. If you want to get the text contents of the entry, print entry1.get() instead.

You don't need the global statements. Those are only useful if you're reassigning the value of entry1 inside a function, which isn't necessary here.

from Tkinter import *

root = Tk()

def printentry1():
    print entry1.get()

entry1 = Entry(root)
button = Button(root,text="Submit",command=printentry1)
entry1.pack()
button.pack()
root.mainloop()
Kevin
  • 74,910
  • 12
  • 133
  • 166