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:
Now type filedialog. Type a dot and you'll see a menu appear. This one:
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:
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:
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:
But the top of your code should look like this (You can spread code over more than one line, as we have done):
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:
Click the file types at the bottom:
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.
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)
Your form will then look like this when its run:
Click the Edit menu to see this:
In the next lesson, you'll learn about the Text Widget. You'll need this when we open a text file.