Skip to content Skip to sidebar Skip to footer

How To Pass The Selected Filename From Tkfiledialog GUI To Another Function

I'm building a simple desktop application using Tkinter that has a browse button for the user to be able to select file from their computer (below code is in a file called gui.py):

Solution 1:

What i think you meant

This is a solution for what I assume that you mean when you say that you want the name of the selected file passed to predict.py

Inside predict.py, make these changes;

First import the GUI

from gui import simpleapp_tk

Second, make the main function be passed an argument

def main(filename="test.foo"):

Lastly, in your run routine, instantiate the GUI, get the filename, and run the main function

if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('my application')
    app.mainloop()
    # Here it will wait until the GUI finishes and exits
    filename = app.labelVariable.get()
    main(filename)

What also can be done

You could from the gui.py import predict.py and change the method call of the "Go" button.

or,

If you make button_browse an attribute of simpleapp_tk, you could easily set the command of it externally if you would import the GUI elsewhere, this is inside predict.py

if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('my application')
    app.configure(command=main(tkFileDialog.askopenfilename()))
    app.mainloop()

Post a Comment for "How To Pass The Selected Filename From Tkfiledialog GUI To Another Function"