0

I am trying to save some names and values from 6 texboxes, 2 are Strings and 4 are Integers, in .txt or .doc but it is not working

I am using python 3.4 in windows 8.1, can somebody help me, I have not being able to find any info or example in python 3.4 to save whatever I write in those 6 texboxes I have a button to call the function mSave

I have the following code to save but is not working

def mSave():
    filename = asksaveasfilename(defaultextension='.txt',
                                 filetypes=(('Text files', '*.txt'),
                                            ('Python files', '*.py *.pyw'),
                                            ('All files', '*.*')))
  if filename:
     with open(filename, 'w') as stream:
          stream.write(self.gettext())

Python says

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\idlelib\run.py", line 121, in main
    seq, request = rpc.request_queue.get(block=True, timeout=0.05)
  File "C:\Python34\lib\queue.py", line 175, in get
    raise Empty
queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
  File "C:\\ManualTab.py", line 608, in mSave
    filename = asksaveasfilename(defaultextension='.txt',
NameError: name 'asksaveasfilename' is not defined

pleae help

1 Answers1

0

I have found the answer

ok first of all, for python 3.4 I have to use

from tkinter import *

but it does NOT mean that it will import ALL so I have to call

from tkinter import filedialog
from tkinter.filedialog import asksaveasfilename

and it is the same with open if somebody need it

from tkinter.filedialog import askopenfilename

NOW FINALLY I CAN CALL MY FUNCTION

def mSave():
  filename = asksaveasfilename(defaultextension='.txt',filetypes = (('Text files', '*.txt'),('Python files', '*.py *.pyw'),('All files', '*.*')))
  if filename is None:
    return
  file = open (filename, mode = 'w')
  name1 = TubeLenghtVal_1.get()          
  name2 = TubeLenghtVal_2.get()

  # or whatever you assign the name of the variable, for me is TubeLenghtVal_2 = StringVar() and for integer I have Vol_Val = IntVar()
  all = name1 + " , " + name + " , " + (str(Vol_Val))
  file.write(all)
  file.close()

and that is all