Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save File Dialog in Tkinter

Tags:

python

tkinter

I am implementing a GUI based text editor in python.
I have displayed the text area but when I try to use the asksaveasfile method in Tkinter, it shows that the file has been saved but when I try and open the same file in my desktop editor, it gives me a blank file.

Only, the file is created and saved. Its contents are not.

I would like to know why. Am I doing something wrong? Here is my code:

from Tkinter import *
import tkMessageBox
import Tkinter
import tkFileDialog

def donothing():
   print "a"

def file_save():
    name=asksaveasfile(mode='w',defaultextension=".txt")
    text2save=str(text.get(0.0,END))
    name.write(text2save)
    name.close

root = Tk()
root.geometry("500x500")
menubar=Menu(root)
text=Text(root)
text.pack()
filemenu=Menu(menubar,tearoff=0)
filemenu.add_command(label="New", command=donothing)
filemenu.add_command(label="Open", command=donothing)
filemenu.add_command(label="Save", command=file_save)
filemenu.add_command(label="Save as...", command=donothing)
filemenu.add_command(label="Close", command=donothing)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)

editmenu=Menu(menubar,tearoff=0)
editmenu.add_command(label="Undo", command=donothing)
editmenu.add_command(label="Copy", command=donothing)
editmenu.add_command(label="Paste", command=donothing)
menubar.add_cascade(label="Edit", menu=editmenu)

helpmenu=Menu(menubar,tearoff=0)
helpmenu.add_command(label="Help",command=donothing)
menubar.add_cascade(label="Help",menu=helpmenu)

root.config(menu=menubar)
root.mainloop()  
like image 522
Rohit Shinde Avatar asked Oct 20 '13 10:10

Rohit Shinde


People also ask

How do I save in tkinter?

By simply clicking on the save button we can save any file. Code: In the following code, we create a widget inside the widget a button is placed. By clicking on the button a dialogue box is open inside the dialog box we can save the file by simply click on the Save button.

How do you display a file dialog for opening a file in Python?

Use the askopenfilename() function to display an open file dialog that allows users to select one file. Use the askopenfilenames() function to display an open file dialog that allows users to select multiple files.

What is file dialog in Python?

File dialogs help you open, save files or directories. This is the type of dialog you get when you click file,open. This dialog comes out of the module, there's no need to write all the code manually. Tkinter does not have a native looking file dialog, instead it has the customer tk style.

Which function can be used on the file to display a dialogue for saving a file?

asksaveasfile() is the function which is used to save user's file (extension can be set explicitly or you can set default extensions also). This function comes under the class filedialog . Output #2: Dialogbox when user presses the save button (dialog box to save file is opened).


2 Answers

The function name is asksaveasfilename. And it should be qualified as tkFileDialog.asksaveasfilename. And it does not accept mode argument.

Maybe you want to use tkFileDialog.asksaveasfile.

def file_save():
    f = tkFileDialog.asksaveasfile(mode='w', defaultextension=".txt")
    if f is None: # asksaveasfile return `None` if dialog closed with "cancel".
        return
    text2save = str(text.get(1.0, END)) # starts from `1.0`, not `0.0`
    f.write(text2save)
    f.close() # `()` was missing.
like image 178
falsetru Avatar answered Oct 02 '22 06:10

falsetru


For some reason, when I write:

CodeToSave=str(text.get(1.0, END))

"text" is marked red and it says it's an unresolved reference

like image 39
Fabian Avatar answered Oct 02 '22 06:10

Fabian