[Tutor] Entry in Tkinter

Abel Daniel abli@freemail.hu
Wed Mar 26 14:48:02 2003


Diego Prestes wrote:
> Hello,
>    Im trying tu use the Entry widget. I type a word in the Entry and in 
> find1 module it use the entry using the option self.proc.get(), but when 
> I use this, I have this error:
> 
>    AttributeError: 'NoneType' object has no attribute 'get'
> 
>    In a test I try to use the Entry and it works normally, but in my 
> program not.
>    Someone know what I could be doing wrong?
> 
> self.find1 = Button(frame3, text="Find", command=self.find1).pack(side=LEFT)
> self.proc = Entry(frame3, background="white").pack(side=LEFT)
> 
> def find1(self):  #count the number of times that the word in the text
>        vpal = count(self.text,self.proc.get())
>        tkMessageBox.showinfo("Find","%s and %d" % (self.proc.get(),vpal))
Your problem is that you store the result of the pack() method instead
of the widget instance. pack() returns None, so when you want to call
self.proc.get() in your find1 method, you are trying to access an
attribute of None giving the AttributeError you saw.

Some examples with the interactive interpreter:
>>> from Tkinter import *
>>> box=Entry()
>>> type(box)
<type 'instance'>
>>> box
<Tkinter.Entry instance at 0x80f3c9c>
>>> p=box.pack()
>>> p
>>> type(p)
<type 'None'>
>>> box.get()
'asdf'
>>> # which was the text I typed in.
>>> p.get()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  AttributeError: 'None' object has no attribute 'get'
>>> 

A second problem will be that in the code you posted you rebind self.find1
when making the Button. Something like this should work:

self.find_button = Button(frame3, text="Find", command=self.find1)
self.find_button.pack(side=LEFT)
self.proc = Entry(frame3, background="white")
self.proc.pack(side=LEFT)

your find1 method looks ok.

--
Abel Daniel