1

I'm trying to save a big list of variables into a text document, but I am getting errors when I click on the "save" button either when I'm using asksaveasfile or asksaveasfilename.

The relevant part of code (it picks about a hundred different variables, but to save space I won't write all of them) is:

from tkinter import *
from tkinter.filedialog import *
def save_doc():
    text_file=open(asksaveasfile, mode ='w')
    text_file.write(nombrePatrocinado+"\n")
    text_file.write(apellidoPaternoPatrocinado+"\n")
    text_file.close()
saveDocBTN=Button(text="Save", command=save_doc)
saveDocBTN.grid(row=0,column=6)

When I use this, it says

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1536, in __call__
    return self.func(*args)
  File "/home/juanelo/Downloads/launcher.py", line 662, in save_doc
    text_file=open(asksaveasfile, mode ='w')

TypeError: invalid file: <function asksaveasfile at 0xb5e973d4>

The other one I tried is pretty much the same:

from tkinter import *
from tkinter.filedialog import *
def save_doc():
    text_file=open(asksaveasfilename)
    text_file.write(nombrePatrocinado+"\n")
    text_file.write(apellidoPaternoPatrocinado+"\n")
    text_file.close()
saveDocBTN=Button(text="Save", command=save_doc)
saveDocBTN.grid(row=0,column=6)

When I try this, I get

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1536, in __call__
    return self.func(*args)
  File "/home/juanelo/Downloads/launcher.py", line 662, in save_doc
    text_file=open(asksaveasfilename)
TypeError: invalid file: <function asksaveasfilename at 0xb5df82b4>
Juan Dougnac
  • 95
  • 10

1 Answers1

2

You aren't calling asksaveasfilename(); you are just referencing it. You need to add parentheses:

text_file=open(asksaveasfilename())
zondo
  • 19,901
  • 8
  • 44
  • 83
  • I did that and it opens the dialog box, but it shows an error when I try to create a new file to save: FileNotFoundError: [Errno 2] No such file or directory: '/home/juanelo/Downloads/ddd.txt' – Juan Dougnac Feb 22 '16 at 02:36
  • You need to tell python to create it if it doesn't exist: `text_file = open(asksaveasfilename(), 'w')` – zondo Feb 22 '16 at 02:38