Python Save As


In the previous lesson, you learned how to save a file. In this lesson, we'll get our Save As menu option working. Our Save As menu option is a lot easier to code for. We only need a few lines of Python code. First, set up a new function in your code. Call it savefileas:

def savefileas():

As the first line of this function, we can display the Save As dialog box:

save_text_as = filedialog.asksaveasfile(mode='w', defaultextension='.txt')

The filedialog has a function called asksaveasfile. There are lots of parameters you can add between the round brackets of asksaveasfile. The two we need are the mode, which just 'w' for write, and one called defaultextension. The defaultextension is set to .txt. This extension will get added automatically in the filename area of your dialog box.

Next, we can the text from the text area. This is the same code as before:

text_to_save = textArea.get('1.0', 'end-1c')

To write the text to file, we can use the write function on a file object:


Finally, close the file object:


We can wrap this all in an if statement:

if save_text_as:

text_to_save = textArea.get('1.0', 'end-1c')


messagebox.showinfo("Error", "Cancelled")

Using an if statement means we can check if the cancelled button is clicked on the dialog box. If it is, then save_text_as will be false (or you can test for None: if save_text_as is None:).

The top of your code in PyCharm should look like this:

Python function to save a text file

Before trying it out, locate this line in your menu creation code:

fileMenuItems.add_command(label="Save As", command=openfile)

Change it to this:

fileMenuItems.add_command(label="Save As", command=savefileas)

Try it out. Open up a text file. Now click File > Save As on your menu. You should see a Dialog box appear. When you do, click the cancel button. You should see your error message:

Error message box in Tkinter

Now try again. When you see the Save As dialog box, it should look something like this (Windows 10):

Windows Save As dialog box

Notice that there are no Save as types at the bottom. You can add some when setting up your dialog box, just like you did with the open file dialog box. You can even add a title, as you did before.

The Close menu

Since we have a Close item on our menu, we'll deal with that. It only needs one line of code, really. What you can do here is just delete the contents of the text area. (We've already closed the file objects when from our functions.)

Create a new function. Call it closefile. The only code you need is a line to clear the text area. You've met this before. Here's your function, then:

def closefile():

textArea.delete('1.0', tk.END)

Now locate this line in your menu creation code:

fileMenuItems.add_command(label="Close", command=openfile)

Change it to this:

fileMenuItems.add_command(label="Close", command=closefile)

Try it out. When you run your program, click File > Close. The default text in the text area should disappear leaving you with an empty text box.

We'll leave file manipulation there and move on. Next, we'll tackle importing files.

Python Modules >

Python Modules >