get() attribute for Entry in Tkinter

R

Robert Upton

Dear Pythoners,

I am in the process of generating a simple GUI that wants to read a
string and print it to the terminal after engaging a button. I am
running into a problem where Python says it does not understand the
get() attribute for Entry. My code is very simple and is shown
below. Please provide comments.

Thankyou

import Tkinter
from Tkinter import *

def Test():
global widgetEntry
strVal = widgetEntry.get()
print str(strVal)


root = Tk() # opens a
new window
textFrame = Frame(root)

widgetLabel = Label(textFrame)
widgetLabel.config(text = 'Enter text')
widgetLabel.pack(side = LEFT)
widgetLabel.pack(expand = YES, fill = BOTH)

widgetEntry = Entry(textFrame).pack(side = LEFT)
textFrame.pack()
Button(root, text = 'Submit', command=Test).pack(side=LEFT)

root.mainloop()
 
P

Peter Otten

Robert said:
I am in the process of generating a simple GUI that wants to read a
string and print it to the terminal after engaging a button. I am
running into a problem where Python says it does not understand the
get() attribute for Entry.

Please don't paraphrase Python's error messages. Cut-and-paste the traceback
instead. This should give something like

$ python tmp_tk.py
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in __call__
return self.func(*args)
File "tmp_tk.py", line 6, in Test
strVal = widgetEntry.get()
AttributeError: 'NoneType' object has no attribute 'get'

So the value of widgetEntry is None. How could that be?
widgetEntry = Entry(textFrame).pack(side = LEFT)

The pack() method returns None; if you need a reference to the Entry widget
you have to split that line

widgetEntry = Entry(textFrame)
widgetEntry.pack(side=LEFT)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top