0

I am trying to implement two functions save and save as in my text editor with wxPython. My save function should work so that if writing to an existing file, the save is done automatically, without the file save dialog. But my function does not work like that for some reason. I guess that the try block is not being executed, I even tried to display the error on the screen but I cannot see that error. Please help me!

import os
import wx

class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(800,600))
        self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
        self.CreateStatusBar()
        filemenu = wx.Menu()
        menuOpen = filemenu.Append(wx.ID_OPEN, "&Open"," Open a file")
        menuSave = filemenu.Append(wx.ID_SAVE, "&Save"," Save a file")
        menuSaveAs = filemenu.Append(wx.ID_SAVE, "&Save as"," Save a file as")
        menuAbout = filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
        menuExit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&File")
        self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.
        self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
        self.Bind(wx.EVT_MENU, self.OnSave, menuSave)
        self.Bind(wx.EVT_MENU, self.OnSaveAs, menuSaveAs)
        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
        self.Show(True)
    def OnOpen(self, e):
        self.dirname = ''
        dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.FD_OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetFilename()
            self.dirname = dlg.GetDirectory()
            f = open(os.path.join(self.dirname, self.filename), 'r', encoding="utf8")
            self.control.SetValue(f.read())
            f.close()
        dlg.Destroy()
    def OnSave(self, e):
        try:
            f = open(os.path.join(self.dirname, self.filename), 'w', encoding='utf8')
            f.write(self.control.GetValue())
            f.close()
        except:
            try:
                dlg = wx.FileDialog(self, "Save to file:", ".", "", "*.*", 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', encoding='utf8')
                    f.write(self.control.GetValue())
                    f.close()
                dlg.Destroy()
            except:
                pass
    def OnSaveAs(self, event):
        try:
            dlg = wx.FileDialog(self, "Save to file:", ".", "", "*.*", 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', encoding='utf8')
                f.write(self.control.GetValue())
                f.close()
            dlg.Destroy()
        except:
            pass
    def OnAbout(self, e):
        dlg = wx.MessageDialog(self, "A Record text editor", "About Sample Editor", wx.OK)
        dlg.ShowModal()
        dlg.Destroy()
    def OnExit(self, e):
        self.Close(True)
app = wx.App(False)
frame = MainWindow(None, "Record books")
app.MainLoop()
  • use wx.ID_SAVEAS for the save as menu item. – toppk Sep 21 '22 at 03:13
  • Thanks, but my question is related to the save function – Constantine Ryzhikov Sep 21 '22 at 03:19
  • you are using the same id for both save and saveas function which is causing the save function to act like saveas. correct the saveas function and you will fix the save function. – toppk Sep 21 '22 at 03:25
  • 1
    You might have found the problem better if you [avoid bare except](https://stackoverflow.com/questions/54948548/what-is-wrong-with-using-a-bare-except) – Psionman Sep 21 '22 at 06:29

0 Answers0