Execute a function right after user has chosen a file through tkFileDialog

443 views Asked by At

I'm developing an interface with Tkinter that makes use of a file dialog with tkFileDialog. I want to run a function immediately after the user has chosen a file from the dialog box.

With buttons, we have a command keyword from which we run a function (usually named def callback():). Is there a similar keyword for the file dialog or askopenfilename?

1

There are 1 answers

0
Right leg On BEST ANSWER

The askopenfilename function consists of the opening of a dialog, and returns immediately when the latter was closed (including when a file has been selected). Put your callback right after this function to have it run right after the closing of the dialog.

For instance:

from tkinter.filedialog import askopenfile

fileDescriptor = askopenfilename()
print(fileDescriptor)

will open a file selection dialog, and as soon as the user has selected a file, the corresponding object that was created will be printed out.