简体   繁体   中英

wxpython save and save as,

Example image
I am trying to solve the save option but my problem is when I press save button, if the file does not exist, it should show a dialog box to ask for a path/file name and then save the file.

  • Sorry for poor English, see the image.

I want it to work as follows:
1) Open new file and write content (Done).
2) Save "if it is a new file, the dialog box has to show up".
3) again press Save "If the file already exist means the dialog box has to disappear and file has to update.

Thanks and regards,
D. Vinay Singh

def onSaveAs(self, event):
    dlg = wx.FileDialog(self, "Save to file:", ".", "", "Text (*.txt)|*.txt", wx.FD_SAVE)
    if dlg.ShowModal() == wx.ID_OK:
        i = dlg.GetFilterIndex()
        if i == 0: # Text format
            try:
                f = open(dlg.GetPath(), "w")
                print(f)
                hole = self.txt.GetValue()
                print(hole)
                f.write(hole)
            except:
                print("Hello")



def onSave(self, event):
    pathtxt = self.txt_1.GetValue()

    f = open(pathtxt,"w")
    hole_1 = self.txt.GetValue()
    f.write(hole_1)

Try this:

import os

        def onSave(self, event):
            try:
                f = open(os.path.join(self.dirname, self.filename), 'w')
                f.write(self.control.GetValue())
                f.close()
            except:
                try:
                    dlg = wx.FileDialog(self, "Save to file:", ".", "", "Text (*.txt)|*.txt", wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
                    if (dlg.ShowModal() == wx.ID_OK):
                        self.filename = dlg.GetFilename()
                        self.dirname = dlg.GetDirectory()
                        f = open(os.path.join(self.dirname, self.filename), 'w')
                        f.write(self.control.GetValue())
                        f.close()
                    dlg.Destroy()
                except:
                    pass

        def onSaveAs(self, event):
            try:
                dlg = wx.FileDialog(self, "Save to file:", ".", "", "Text (*.txt)|*.txt", wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
                if (dlg.ShowModal() == wx.ID_OK):
                    self.filename = dlg.GetFilename()
                    self.dirname = dlg.GetDirectory()
                    f = open(os.path.join(self.dirname, self.filename), 'w')
                    f.write(self.control.GetValue())
                    f.close()
                dlg.Destroy()
            except:
                pass

Note: self.filename and self.dirname needs to be initiated and tracked at all times.

Try something like this:
Note: I haven't tested it

def onSave(self, event):
    pathtxt = self.txt_1.GetValue()
    if pathtxt != "":
        if not pathtxt.endswith('.txt'):
            pathtxt=pathtxt+'.txt'

    try:
        with open(pathtxt, 'w') as f:
            f.write(self.txt.GetValue())
    except:
        try:
            dlg = wx.FileDialog(self, "Save to file:", ".", "", "Text (*.txt)|*.txt", wx.FD_SAVE)
            if dlg.ShowModal() == wx.ID_OK:
                i = dlg.GetFilterIndex()
                if i == 0: # Text format
                    try:
                        with open(dlg.GetPath(), 'w') as f:
                            f.write(self.txt.GetValue())
                    except:
                        print("Save failed")
                else:
                    print("Save failed - Use .txt file suffix")
        except:
            print("Save failed - Unknown reason")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM