views:

2353

answers:

2

Hi

I have seen a couple of other posts on similar error message but couldn't find a solution which would fix it in my case.

I dabbled a bit with TkInter and created a very simple UI. The code follows-

from string import *
from Tkinter import *
import tkMessageBox

root=Tk()
vid = IntVar()

def grabText(event):
    if entryBox.get().strip()=="":
     tkMessageBox.showerror("Error", "Please enter text")
    else:
     print entryBox.get().strip()    

root.title("My Sample")
root.maxsize(width=550, height=200)
root.minsize(width=550, height=200)
root.resizable(width=NO, height=NO)    

label=Label(root, text = "Enter text:").grid(row=2,column=0,sticky=W)
entryBox=Entry(root,width=60).grid(row=2, column=1,sticky=W)
grabBtn=Button(root, text="Grab")
grabBtn.grid(row=8, column=1)
grabBtn.bind('<Button-1>', grabText)

root.mainloop()

I get the UI up and running. When I click on the 'Grab' button, I get the following error on the console-

C:\Python25>python.exe myFiles\testBed.py
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
    return self.func(*args)
  File "myFiles\testBed.py", line 10, in grabText
    if entryBox.get().strip()=="":
AttributeError: 'NoneType' object has no attribute 'get'

The error traces back to Tkinter.py. I'm relatively new to Python, so couldn't really make sense out of it.

I'm sure some one might have dealt with this before. Any help is appreciated.

cheers

+10  A: 

I'm not that good with Tkinter, but as I understand it, the grid function of the Entry object returns None. You should do that on two lines, like this:

...
entryBox=Entry(root,width=60)
entryBox.grid(row=2, column=1,sticky=W)
...

That way, you get your Entry box, and it's laid out like you expect.

bluejeansummer
Thanks. That helps.
Andriyev
+2  A: 

Change the line:

entryBox=Entry(root,width=60).grid(row=2, column=1,sticky=W)

into the two lines:

entryBox=Entry(root,width=60)
entryBox.grid(row=2, column=1,sticky=W)

(and the same for label btw -- just as you already correctly do for grabBtn!-).

Alex Martelli
Thanks Alex. I should have thought of that :-)
Andriyev