Home Python Course Menu Book

Python Tkinter File Dialog Boxes

 

What we'll do now is to display an open file dialogue box so that we can select a text file that we want to open. We'll just print out the file name, for the time being. Later, we'll actually open up the file and place the contents into a text area.

The first thing you need to do is to add an import line to the top of your code. Add this just below import tkinter as tk:

from tkinter import filedialog

This line ensures that we can access the filedialog library, which is part of tkinter.

On the first line of your function, just before the print line, set up a new variable to hold a file name:

filename =

Now type filedialog. Type a dot and you'll see a menu appear. This one:

PyCharm context menu showing the Tkinter askopenfilename option

Select askopenfilename from the list. As its name suggests, this gets you an open file dialog box where you can select a file name.

In between the round brackets of askopenfilename we need at least three things: an initial directory, a title and some file types. The initial directory is the one the dialog box will go to when it is launched, one that display files and folders. The title will appear at the top of the dialog box. The file types are the ones that you want the dialog box to open. You can add a list of types here: txt files, doc files, etc.

Add the following between the round brackets of askopenfilename:

askopenfilename(initialdir="/", title="Open File", filetypes=(("Text Files", "*.txt"), ("All Files", "*.*")))

For the initialdir parameter we have "/". This gets you to the root directory on most systems. You can enter a hard-coded path, if you want. But they tend to be operating system specific. For example, on windows you could enter this:

"C:/Users/Ken/Documents/Python"

However, other operating systems may not recognise this file path.

Careful when adding the file types. The format is this:

("Text Files", "*.txt")

A comma separates the text that will display in the file types dropdown menu and the file extension that comes at the end of files. Both have their own quote marks. Notice, too, where all the round brackets are above.

Instead of printing "File Open" print the filename:

print(filename)

But the top of your code should look like this (You can spread code over more than one line, as we have done below):

Python code to open a Tkinter dialog box

You can test it out now. Run your program. Click your File > Open menu. You should see an Open File dialog box appear. This is the one from Windows 10:

An Open File dialog box on Windows

Click the file types at the bottom:

The file types section of a dialog box

You can see the two items we added. Text Files and All Files.

Select a file to open. When you click the Open button, you should see a file name and path printed to your output window.

 

Save Dialog Boxes

There are two save options with file dialog boxes. One is called asksaveasfile and asksaveasfilename. The difference between the two is that the latter option gets you a file dialog where you can save your file with a different name and the former just gets on with the job of saving a file. We'll do this in a later section, when we move on to dealing with text files.

Other menus

You can set up another main menu in the same way. For example, here's the code to set up an Edit menu next to the File menu. Add it just after the File menu code but before the form.config line. (No command parameters have been set up, so the menu won't do anything):

editMenu = tk.Menu(menuBar, tearoff=0)
editMenu.add_command(label="Cut")
editMenu.add_command(label="Copy")
editMenu.add_command(label="Paste")

menuBar.add_cascade(label="Edit", menu=editMenu)

Your form will then look like this when its run:

An Edit menu set up in TKinter

Click the Edit menu to see this:

A dropdown Edit menu in TKinter

 

In the next lesson, you'll learn about the Text Widget. You'll need this when we open a text file.