How can the variable be used by other functions in TkfileDialog?

45 views Asked by At

I intended to write a GUI to import URLs data then process these data, so I had 2 buttons. Below is my code.

from Tkinter import *

    root=Tk()
    root.title('Videos Episodes')
    root.geometry('500x300')

    def OpenFile():  # import URLs data from local machine
        paths=tkFileDialog.askopenfilename()
        return paths

    def read_files(paths): #read data from the directory from OpenFile
        with open(paths) as myfile:
                    return data 
    Button(root,text='Input',command=OpenFile).pack()
    Button(root,text='Process',command=read_files).pack()
    root.mainloop()

My problem is that when 'Process' button clicked, error happened: Exception in Tkinter callback Traceback (most recent call last):

File "C:\Python27\lib\lib-tk\Tkinter.py", line 1532, in __call__
    return self.func(*args) TypeError: read_files() takes exactly 1 argument (0 given)

How can I fix the bug?

1

There are 1 answers

1
fferri On

If you want to pass an argument (you didn't specify what), use a lambda:

Button(root,text='Process',command=lambda: read_files('whatever')).pack()

Perhaps, this is what you wanted to do (?):

Button(root,text='Process',command=lambda: read_files(OpenFile())).pack()

or alternatively, you meant to store the result of OpenFile (from clicking the other button) in a global variable, and pass that as argument of read_files...?