Reputation: 623
I'm having problems with my Tkinter Entry widget. I'm just testing things out and would like to have my callback print out whatever I typed out in Entry self.a. but I'm getting this error.
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in call return self.func(*args) File "C:/Users/Andy/testimage.py", line 146, in get print a.get(self) NameError: global name 'a' is not defined
I was wondering if someone can tell me what I'm doing wrong. I linked the callback function correctly because if I make it print "aasdfasd" instead, it will print that when I press the button.
def clicked_wbbalance(self):
self.top = Toplevel()
self.top.title("LASKJDF...")
Label(self.top, text="Enter low level").grid(row=0, column=0,padx=10)
Label(self.top, text="Enter high level").grid(row=1, column=0,padx=10)
Label(self.top, text="Values must be between 0 to 255").grid(row=3, column=0)
Button(self.top, text="Ok", command=self.get).grid(row=3, column = 1)
self.a =Entry(self.top).grid(row=0, column=1,padx=10)
self.b =Entry(self.top).grid(row=1, column=1,padx=10)
def get(self):
print self.a.get(self)
Upvotes: 0
Views: 1690
Reputation: 45542
As RocketDonkey pointed out, your traceback does not match the code you posted.
Your code as written will generate a traceback like this:
AttributeError: 'NoneType' object has no attribute 'get'
The root problem is that grid
returns None
. That means that attributes a
and b
will be None
because they are assigned the result of calls to grid
. Fix that by puting object creation and widget placement on different lines:
self.a = Entry(self.top)
self.b = Entry(self.top)
self.a.grid(row=0, column=1,padx=10)
self.b.grid(row=1, column=1,padx=10)
Upvotes: 3
Reputation: 37249
You traceback says print a.get(self) NameError: global name 'a' is not defined
, but the code you posted uses the syntax print self.a.get(self)
(which would appear to be correct). Therefore if you check on line 146, you aren't prefacing a
with self
, meaning that instead of referencing the property a
of the instance, you are trying to reference a
on its own, which is not defined. Try adding self
in front of a
on line 146 and see if the problem continues.
Upvotes: 1