when I initiate second button click -> AttributeError: Application instance has no attribute 'readfile'
Question: 
    from Tkinter import *
    
    class Application(Frame):
        def __init__(self, master):
            Frame.__init__(self, master)
            self.grid() #put it on the grid
            self.create_widgets()
        #widget
        def create_widgets(self):
            self.button1 = Button(self)
            self.button1.grid(row = 3, column = 5, columnspan = 2, stick = E)
            self.button1["text"] = "Browse"
            self.button1["command"] = self.update_count
            self.pack()
    #here
            self.text1 = Text(self)
            self.text1 = Text(self, width = 41, height = 1, wrap = WORD)
            self.text1.grid(row = 3, column = 1, columnspan = 2, stick = W)
    # here
            self.button2 = Button(self)
            self.button2.grid(row = 5, column = 5, columnspan = 2, stick = E)
            self.pack()
            self.button2["text"] = "OK"
            self.button2["command"] = self.readfile
    
           # select list box
            self.listbox1 = Listbox(self, selectmode = EXTENDED)
            self.listbox1 = Listbox(self, width = 51, height = 5)
            self.listbox1.grid(row = 5, column = 1, columnspan = 2, stick = W)
    
            self.label1 = Label(self)
            self.label1.grid(row = 1, column = 1, columnspan = 2, sticky = W)
            self.label1["text"] = "Choose the data log file folder ..."
    
            self.label2 = Label(self)
            self.label2.grid(row = 4, column = 1, columnspan = 2, sticky = W)
            self.label2["text"] = "Choose the data log file ..."
    
        #on click
        def update_count(self):
    
            path_string = tkFileDialog.askdirectory()     
            self.text1.delete(0.0, END)
            self.text1.insert(0.0, path_string)
        # read files
            onlyfiles = [ f for f in listdir(path_string) if isfile(join(path_string,f)) ]
            with open("output.txt", "w") as a:
                for path, subdirs, files in os.walk(path_string):
                   for filename in files:
                         ffnames = os.path.join(filename)
                         self.listbox1.insert(END, ffnames)
                         a.write(str(ffnames) + os.linesep)
            #on click
            def readfile(self):
                global listEx
                items = map(int, listbox1.curselection())
                result = listEx[items[0]]
                print result
        # main loop
    
        root = Tk() 
        root.title("Test Window")
        root.geometry("900x700")
    
        app = Application(root)
    
        root.mainloop() #main loop
    

Thanks in advance,

Can anyone help me to fix this error, I have got two button, first button
works, now I need to read a file name from the listbox so have declared a
constructor to do the work, eventually I got an AttributeError, don't know
why?

I am new to Python, as a beginner struggling to fix, please help me.

Ramas


Answer: The error is telling you exactly the problem -- your class has no method named
`readfile`. It looks like you have indented the method incorrectly, causing it
to be private to the `update_count` method. The solution is to remove one
level of indentation to the function definition, so that it is in the proper
scope.

