python logo

tkinter filedialog


Python hosting: Host, run, and code Python in the cloud!

tkFileDialog in Tkinter provides functionality for open and save dialog functions, allowing developers to easily integrate file selection features in their Tkinter GUI applications.

On this page, you’ll find a comprehensive guide to tkinter file dialogs. This includes sample code snippets accompanied by illustrative screenshots. If you’ve ever struggled with finding concise examples or clear instructions for implementing file dialogs in Tkinter, this article aims to resolve that.

For those interested in diving deeper into building desktop applications using Tkinter, consider the following course:

Open File Dialog in Tkinter

The askopenfilename function is used to initiate an open file dialog in Tkinter. This function will display file types and their respective extensions at the bottom of the dialog, helping users to filter their file choices. This functionality in Tkinter is an extension of the askopenfilename function available in Tcl/Tk.

The following code showcases how to invoke the open file dialog in both Python 2.7 and Python 3. After executing the code, if the user clicks on ‘cancel’, the returned filename will be empty. Windows users should modify the initialdir parameter value to “C:\” for optimal performance.

Python 2.7:

1
2
3
4
5
6
from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog

root = Tk()
root.filename = tkFileDialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
print (root.filename)

Python 3:

1
2
3
4
5
6
from tkinter import filedialog
from tkinter import *

root = Tk()
root.filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
print (root.filename)

tkfiledialog Tkinter askopenfilename

Save File Dialog in Tkinter

The asksaveasfilename function is instrumental in generating a save file dialog in Tkinter.

Here’s how you can utilize this function in both Python 2.7 and Python 3:

Python 2.7:

1
2
3
4
5
6
from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog

root = Tk()
root.filename = tkFileDialog.asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
print (root.filename)

Python 3:

1
2
3
4
5
6
from tkinter import filedialog
from tkinter import *

root = Tk()
root.filename = filedialog.asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
print (root.filename)

Directory Selection in Tkinter

There might be cases where your application requires the user to select a directory rather than a file. In such scenarios, the askdirectory function can be employed to present the user with a directory selection popup.

Python 2.7:

1
2
3
4
5
from  Tkinter import *
import Tkinter, Tkconstants, tkFileDialog
root = Tk()
root.directory = tkFileDialog.askdirectory()
print (root.directory)

tkinter askdirectory

For those keen on expanding their knowledge with practical Tkinter examples, feel free to download additional tkinter examples.

Navigate through our Tkinter tutorials: < Back | Next >






Leave a Reply: