File Dialog boxes in Tkinter
File dialog boxes are pop-up windows that allow users to choose files or directories from their computer’s file system. These dialogs provide a graphical interface for file selection, making it easier for users to navigate and select files without needing to type the file path manually.
The filedialog
submodule in Tkinter helps us by providing simple, user-friendly dialogs for file and directory selection. This can be particularly useful in applications where users need to open, save, or browse files.
askdirectory()
The askdirectory()
function of filedialog
opens a dialog box that lets users select a directory from their file system. Once a directory is chosen, the function returns the path of that directory as a string. If the user cancels the dialog, it returns an empty string.
This function has several optional parameters, here are the main ones:
initialdir | Sets the initial directory that the dialog will open to. By default, it starts in the current working directory. |
title | Sets the title of the dialog window. |
parent | Sets the parent window for the dialog. |
Example :
from tkinter import * from tkinter.ttk import * from tkinter import filedialog root = Tk() root.grid_anchor("center") def open_select_directory_dialog(): select_directory=filedialog.askdirectory(parent=root,title='New title for select directory dialog box') lab=Label(root,text=f'{select_directory}',padding=(50, 10),font="Arial 14 bold",background='yellow') lab.grid(row=0,column=1,pady=5) btn1=Button(root,text='Open Select Directory Dialog box',command=open_select_directory_dialog, padding=15) btn1.grid(row=0,column=0,pady=5) root.mainloop()
Output :
askopenfilename()
The askopenfilename()
function displays a dialog box that enables users to choose a single file from their file system. The function returns the file’s path as a string once a selection is made. If the user cancels the operation, an empty string is returned.
Here are the main parameters of this function :
initialdir | Sets the initial directory when the dialog opens. |
title | Sets a custom title for the dialog window. |
filetypes | Allows you to specify the types of files that can be displayed in the dialog. It takes a list of tuples, where each tuple contains a description and a file extension pattern. Example – filetypes=(("Text files", ".txt"), ("All files", ".*")) |
typevariable | Associates a Tkinter variable (usually a StringVar ) with the file dialog. This variable will hold the type of file selected. |
Example :
from tkinter import * from tkinter.ttk import * from tkinter import filedialog root = Tk() root.grid_anchor("center") def open_openfilename_dialog(): ftypes=(('jpg files','.jpg'),("png files",'.png')) var1=StringVar() openfilename=filedialog.askopenfilename(parent=root,filetypes=ftypes,title='New title for open file name dialog box',typevariable=var1) lab=Label(root,text=f'{openfilename}--{var1.get()}',padding=(50, 10),font="Arial 14 bold",background='yellow') lab.grid(row=1,column=1,pady=5) btn2=Button(root,text='Open openfilename Dialog box',command=open_openfilename_dialog, padding=15) btn2.grid(row=1,column=0,pady=5) root.mainloop()
Output :
askopenfilenames()
The askopenfilenames()
function is similar to askopenfilename()
, but it allows users to select multiple files instead of just one. It takes the same parameters.
Example :
from tkinter import * from tkinter.ttk import * from tkinter import filedialog root = Tk() root.grid_anchor("center") def open_openfilenames_dialog(): ftypes=(('jpg files','.jpg'),("png files",'.png')) var2=StringVar() openfilenames=filedialog.askopenfilenames(parent=root,filetypes=ftypes,title='New title for open file names dialog box',typevariable=var2) lab=Label(root,text=f'{openfilenames}--{var2.get()}',padding=(50, 10),font="Arial 14 bold",background='yellow',wraplength=320) lab.grid(row=2,column=1,pady=5) btn3=Button(root,text='Open openfilenames Dialog box',command=open_openfilenames_dialog, padding=15) btn3.grid(row=2,column=0,pady=5) root.mainloop()
Output :
askopenfile()
The askopenfile()
lets users pick a file and give back an open file object, not just its path. This is handy for directly reading or editing the file’s contents in your Python program. Along with parameters like initialdir
, title
, filetypes
, and parent
, it takes one more parameter.
mode
: Specifies the mode in which the file is opened. It can be 'r'
(read), 'w'
(write), 'a'
(append), or 'r+'
(read and write).
Example :
from tkinter import * from tkinter.ttk import * from tkinter import filedialog root = Tk() root.grid_anchor("center") def open_openfile_dialog(): ftypes=(('jpg files','.jpg'),("png files",'.png')) var3=StringVar() openfile=filedialog.askopenfile(mode="r",parent=root,filetypes=ftypes,title='New title for open file dialog box',typevariable=var3) lab=Label(root,text=f'{openfile}--{var3.get()}',padding=(50, 10),font="Arial 14 bold",background='yellow',wraplength=320) lab.grid(row=3,column=1,pady=5) btn4=Button(root,text='Open openfile Dialog box',command=open_openfile_dialog, padding=15) btn4.grid(row=3,column=0,pady=5) root.mainloop()
Output :
askopenfiles()
With the help of askopenfiles()
function, users can select multiple files and get a list of opened file objects. This is particularly useful when your application needs to work with multiple files simultaneously.
This function also supports the parameters of askopenfile()
.
Example :
from tkinter import * from tkinter.ttk import * from tkinter import filedialog root = Tk() root.grid_anchor("center") def open_openfiles_dialog(): ftypes=(('jpg files','.jpg'),("png files",'.png')) var5=StringVar() openfiles=filedialog.askopenfiles(parent=root,filetypes=ftypes,title='New title for open files dialog box',typevariable=var5) lab=Label(root,text=f'{openfiles}--{var5.get()}',padding=(50, 10),font="Arial 14 bold",background='yellow',wraplength=320) lab.grid(row=4,column=1,pady=5) btn5=Button(root,text='Open openfiles Dialog box',command=open_openfiles_dialog, padding=15) btn5.grid(row=4,column=0,pady=5) root.mainloop()
Output :
asksaveasfilename()
You can use the asksaveasfilename()
function to prompt the user for a filename to save a file. It opens a dialog box where the user can specify the directory, filename, and file extension for saving a file. This method returns the name of the saved file.
You can also use parameters like – filetypes
, title
, and typevariable
with this function.
There is one more argument that you can use with this function – confirmoverwrite
– that will help you to decide whether you want to show the confirm dialog box to overwrite the already saved file
Example :
from tkinter import * from tkinter.ttk import * from tkinter import filedialog root = Tk() root.grid_anchor("center") def open_savefilename_dialog(): ftypes=(('jpg files','.jpg'),("png files",'.png')) var4=StringVar() savefilename=filedialog.asksaveasfilename(parent=root,confirmoverwrite=0,filetypes=ftypes,title='New title for save file name dialog box',typevariable=var4) lab=Label(root,text=f'{savefilename}--{var4.get()}',padding=(50, 10),font="Arial 14 bold",background='yellow',wraplength=320) lab.grid(row=5,column=1,pady=5) btn6=Button(root,text='Open saveasfilename Dialog box',command=open_savefilename_dialog, padding=15) btn6.grid(row=5,column=0,pady=5) root.mainloop()
Output :
asksaveasfile()
The asksaveasfile()
function is much like asksaveasfilename()
, but with a key difference in what it returns. Instead of giving you just the file path as a string, asksaveasfile()
hands you a file object. This object allows you to write directly to the file you’ve selected.
You can also specify an extra parameter with this function called mode
, which tells Python how you intend to open the file.
Example :
from tkinter import * from tkinter.ttk import * from tkinter import filedialog root = Tk() root.grid_anchor("center") def open_savefile_dialog(): ftypes=(('jpg files','.jpg'),("png files",'.png')) var5=StringVar() savefilename=filedialog.asksaveasfile(mode='r',confirmoverwrite=1,parent=root,filetypes=ftypes,title='New title for save fil dialog box',typevariable=var5) lab=Label(root,text=f'{savefilename}--{var5.get()}',padding=(50, 10),font="Arial 14 bold",background='yellow',wraplength=320) lab.grid(row=6,column=1,pady=5) btn7=Button(root,text='Open saveasfile Dialog box',command=open_savefile_dialog, padding=15) btn7.grid(row=6,column=0,pady=5) root.mainloop()