Tutorial :AttributeError in tkinter gui programming



Question:

I want to display my calculated output in a Gui window in python. I am trying with Tkinter. But I'm having problems displaying the output on Tkinter level widget. I am putting input data as address information in text field of Tkinter window and want latitude, longitude of that inputed address to the text label. Can anyone please help me out of this? I am just quite new to this Tkinter.

code is below:

def initialize(self):      self.grid()        self.entry = Tkinter.Entry(self)      self.entry.grid(column=0,row=0,sticky='EW')        button = Tkinter.Button(self,text=u"Get Geo information !",command=self.OnButtonClick)      button.grid(column=1,row=0)        self.labelVariable = Tkinter.StringVar()        label = Tkinter.Label(self,textvariable=self.labelVariable,                            anchor="w",fg="black",bg="white")      label.grid(column=0,row=1,columnspan=2,sticky='EW')        self.grid_columnconfigure(0,weight=1)      self.resizable(True,False)      def OnButtonClick(self):      outf = open(out_file,'w')      outf_failed = open(out_file_failed,'w')      #inf = open(addr_file,'r')      inf = codecs.open(addr_file, 'r', 'iso-8859-1')        for address in inf:          #get latitude and longitude of address          data = geocode(address)            #output results and log to file             if len(data)>1:                 self.labelVariable.set( self.entryVariable.get()+" (Latitude )", data['lat'] )               self.labelVariable.set( self.entryVariable.get()+" (Longitude )", data['lng'] )                 outf.write(address.strip()+data['lat']+','+data['lng']+'\n')                      outf.flush()           else:               self.labelVariable.set( self.entryVariable.get()+" Geocoding of '"+addr_file+"' failed with error code "+data['code'] )                 outf_failed.write(address)                 outf_failed.flush()             time.sleep(sleep_time)        #clean up      inf.close()      outf.close()      outf_failed.close()      if __name__ == "__main__":      app = simpleapp_tk(None)      app.title('Your Location')      app.mainloop()  

And I got error:

File "F:\JavaWorkspace\Test\src\gui_geo_location.py", line 94, in OnButtonClick      self.labelVariable.set( self.entryVariable.get()+" (Latitude )", data['lat'] )    File "C:\Python25\lib\lib-tk\Tkinter.py", line 1721, in __getattr__      return getattr(self.tk, attr)  AttributeError: entryVariable  

Here is my __init__ method:

def __init__(self,parent):          Tkinter.Tk.__init__(self,parent)          self.parent = parent          self.initialize()    def initialize(self):          self.grid()            self.entry = Tkinter.Entry(self)          self.entry.grid(column=0,row=0,sticky='EW')          self.entry.bind("<Return>", self.OnPressEnter)            button = Tkinter.Button(self,text=u"Get Geo information !",command=self.OnButtonClick)          button.grid(column=1,row=0)            self.labelVariable = Tkinter.StringVar()            label = Tkinter.Label(self,textvariable=self.labelVariable,                                anchor="w",fg="black",bg="white")          label.grid(column=0,row=1,columnspan=2,sticky='EW')            self.grid_columnconfigure(0,weight=1)          self.resizable(True,False)  


Solution:1

On line 94 in F:\JavaWorkspace\Test\src\gui_geo_location.py, you're using self.entryVariable but that object does not have an entryVariable attribute.

Based on your __init__, it seems you haven't defined entryVariable anywhere. Try adding:

self.entryVariable = Tkinter.StringVar()  

to your __init__ method. See this page for more information.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »