예제 #1
0
 def on_save(self, evt):
     if self.save_data:
         dlg = wx.FileDialog(self, "Save your session data to file", "", wildcard='*.dvha',
                             style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
         dlg.SetDirectory(DATA_DIR)
         if dlg.ShowModal() == wx.ID_OK:
             self.save_data_obj()
             save_object_to_file(self.save_data, dlg.GetPath())
         dlg.Destroy()
     else:
         wx.MessageBox('There is no data to save. Please query/open some data first.', 'Save Error',
                       wx.OK | wx.OK_DEFAULT | wx.ICON_WARNING)
예제 #2
0
def save_data_to_file(frame,
                      title,
                      data,
                      wildcard="CSV files (*.csv)|*.csv",
                      data_type='string',
                      initial_dir=DATA_DIR):
    """
    from https://wxpython.org/Phoenix/docs/html/wx.FileDialog.html
    :param frame: GUI parent
    :param title: title for the file dialog window
    :type title: str
    :param data: text data or pickle-able object to be written
    :param wildcard: restrict visible files and intended file extension
    :type wildcard: str
    :param data_type: either 'string' or 'pickle'
    :type data_type: str
    :param initial_dir: start the FileDialog at this directory
    :type initial_dir: str
    """

    with wx.FileDialog(frame,
                       title,
                       defaultDir=initial_dir,
                       wildcard=wildcard,
                       style=wx.FD_SAVE
                       | wx.FD_OVERWRITE_PROMPT) as fileDialog:

        if fileDialog.ShowModal() == wx.ID_CANCEL:
            return

        pathname = fileDialog.GetPath()

        if data_type == 'string':
            try:
                with open(pathname, 'w', encoding="utf-8") as file:
                    file.write(data)
            except IOError:
                wx.LogError("Cannot save current data in file '%s'." %
                            pathname)

        elif data_type == 'pickle':
            save_object_to_file(data, pathname)

        elif data_type == 'function':
            data(pathname)

        return pathname