-1

I have some pretty simple code right now that I am having issues with.

root = Tk()
label1 = Label(root, text ="Enter String:")
userInputString = Entry(root)
label1.pack()
userInputString.pack()
submit = Button(root,text = "Submit", command = root.destroy)
submit.pack(side =BOTTOM)
root.mainloop()
print(userInputString)   

When I run the code everything operates as I would expect except

print(userInputString)

for an input asdf in the Entry print will return something like 0.9355325

But it will never be the same value back to back always random.

I am using python 3.5 and Eclipse Neon on a Windows 7 Machine.

Ultimately the goal is to accept a string from the user in the box that pops up and then be able to use that value as string later on. For example, it might be a file path that needs to be modified or opened.

Is Entry not the correct widget I should be using for this? Is there something inherently wrong with the code here? I am new to python and don't have a lot of strong programming experience so I am not even certain that this is set up right to receive a string.

Thanks in advance if anyone has any ideas.

  • 2
    First, what you want to obtain, i.e. the text of the entry, can be obtained by calling the method `get` on the object of type `Entry`, in your case `userInputString`. Second, the code after `root.mainloop()` is executed only after the window is closed.. – nbro Aug 19 '16 at 21:19
  • When do you want to use the string of the entry field? If you specify that I can try to give a specific answer, otherwise I should indicate this question as a duplicate. – nbro Aug 19 '16 at 21:21
  • Possible duplicate of [Tkinter Entry "get" function is returning nothing](http://stackoverflow.com/questions/10727131/tkinter-entry-get-function-is-returning-nothing) – nbro Aug 19 '16 at 21:39
  • @Nuncameesquecideti: it may be a duplicate, but it's not a duplicate of that. This question isn't about it returning nothing, it's about it returning the internal id of the widget (which happens to look like a floating point number even though its not). They are closely related, but different. – Bryan Oakley Aug 19 '16 at 22:50

1 Answers1

1

There are two things wrong with your print statement. First, you print the widget, not the text in the widget. print(widget) prints str(widget), which is the tk pathname of the widget. The '.' represents the root window. The integer that follows is a number that tkinter assigned as the name of the widget. In current 3.6, it would instead be 'entry', so you would see ".entry".

Second, you try to print the widget text after you destroy the widget. After root.destroy, the python tkinter wrapper still exists, but the tk widget that it wrapped is gone. The following works on 3.6, Win10.

import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="Enter String:")
entry = tk.Entry(root)
def print_entry(event=None):
    print(entry.get())
entry.bind('<Key-Return>', print_entry)
entry.focus_set()
submit = tk.Button(root, text="Submit", command=print_entry)
label.pack()
entry.pack()
submit.pack()
root.mainloop()

Bonus 1: I set the focus to the entry box so one can start typing without tabbing to the box or clicking on it.

Bonus 2: I bound the key to the submit function so one can submit without using the mouse. Note that the command then requires an 'event' parameter, but it must default to None to use it with the button.

The NMT Reference, which I use constantly, is fairly complete and mostly correct.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52