what is the right way to implement file-saving dialogue with the traitsui
package from Enthought? At the moment, I have the actual saving function watching for changes in the trait filename_out
(i.e. File
trait). Unsurprisingly, this does nothing when the user wants to save to the same file repeatedly, overwriting it each time. How do I make it save the file each time the user confirms overwriting in the FileEditor dialogue?
A small piece of not-working code:
from traits.api import File, HasTraits
from traitsui.api import FileEditor, View, Item
import numpy
class ArrayToBeSaved(HasTraits):
filename_out = File
traits_view = View(Item('filename_out', editor = FileEditor(dialog_style='save')))
def __init__(self):
self.my_array = numpy.ones(3)
#This is NOT the right way
def _filename_out_changed(self):
numpy.save(self.filename_out, self.my_array)
self.my_array = numpy.zeros(3)
atbs = ArrayToBeSaved()
atbs.configure_traits()
After selecting the file location, the array of ones is saved. After calling the file dialogue one more time, selecting the same file, the user is asked to confirm overwriting. However, nothing happens, as the filename_out
was not changed.
EDIT: I would like to make clear, that the FileEditor does ask to confirm overwriting, but does not save the file.