After the successful update, you have to make sure that the Tkinter module of Python is already configured at your end before using it within the Python script. If it is not yet configured, try to use the following apt install instruction with the keyword python3-tk. You can see that it is already installed.
Example 1:
Now that the Tkinter module is installed successfully, we will use it in our Python code to create a file dialog to select a single file from the current working directory of our Linux system. We create a new Python file named “new.py” in the current working directory first and open it within the text editor to save our time. We import all the sub-classes of the Tkinter module in our Python code at the first line using the “import” keyword followed by the “*” sign.
After that, we import the filedialog class instance from the Tkinter module at the very second line. We call the Tk() class constructor to get its object in a variable “t”. We use the variable “t” to call the geometry function of a Python to set the size for a GUI window to be generated. The same “t” object is used to create a variable filename that gets the name of a file selected from the file dialog via the filedialog instance.
We use the askopenfilename() function of the filedialog class to set the initial directory for the file dialog to be created and title it as “Select File”. We use the current working directory of our system as a value to “initialdir” parameter. The filename is saved to the variable “filename” and the label “l” is created using the Label() function of the Python Tkinter. It is labeled with the “t” object variable with the text “Selected File is:” concatenated with a file name with an exact Courier family bold font size of 11. The label is packed() and the mainloop() executor is called to loop out the Tkinter widgets.
After saving the whole Python code containing a Tkinter file dialog widget, we execute it with the python3 query as shown in the following:
In a while, a file dialog appears in our terminal screen as a Tkinter GUI named “Select File”. To expand its area, we get some Python and Bash files.
To select a file, click on it and tap on the “Open” button as we did in the following demonstration:
After the selection of a Python file from the file dialog, we are directed to the Tkinter main GUI windows where we are shown the path to the file that has been selected.
Example 2:
Now, we will create a new program that will select multiple files from the Tkinter file dialog. For this, we launch the same Python file and the updated whole of its code. We start it with the import of the Tkinter module as “tk” and the ttk object from the Tkinter, Then, we import the file dialog class instance as “f” from the Tkinter module and the message box class of Tkinter. We import the showinfo() function.
The “tk” object calls the Tk() constructor function to store its instance in the variable “t”. The variable “t” is used to add a title for a File Dialog to be created via the “title” function. We call the resizable function of Tkinter to set the resize to False. The size of a dialog is unchanged. The same “t” variable is used to call the geometry() function to set the exact size of a GUI interface to be created.
The “Files()” function is created with the initialization of variable “types” to set the file types to be selected, whether txt or all types. The askopenfilenames() function is used to title the file dialog, initialize the directory, and set the filetypes to be selected. Save the result to a variable “names” via the “f” object of the filedialog class. The showinfo() function is called to title the window that shows the chosen files via the message parameter. The button named “Open Files” is used at the first window of GUI to call the “Files” function to create a file dialog and select multiple files.
After saving the code, we execute it.
The GUI named “File Dialog” appears with the “Open Files” button. Click it to open the dialog.
The dialog named “Open” is opened. Navigate within the specific folder.
Select multiple files by tapping on them one by one and clicking on the Open button.
All three files are displayed on the Chosen Files GUI of Tkinter. Tap “OK” to close it.
Conclusion
That’s all about the use of the Tkinter module of Python in Linux to generate a file dialog in a GUI of a Tkinter. We tried two different examples to create a file dialog in Tkinter GUI. We discussed how a single file or multiple files can be chosen from the directory via the file dialog using different scripts.