Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   help on save and save as methods (http://www.programmingforums.org/showthread.php?t=11844)

Yarvin Nov 9th, 2006 6:15 PM

help on save and save as methods
 
I've been working on this kind of text editor program and I can't figure out how to actually save the date entered into the editor with the save and saveas methods.

heres the two methods that I wrote using other examples from the wxPython site.

:


        def OnSave(self, event):
                """ Save a file"""
                self.dirname = ''
                fDialog = wx.FileDialog(self, "Choose a file", self.dirname, "*.*", wx.SAVE)
                if fDialog.ShowModal() == wx.ID_OK:
                        self.filename = fDialog.GetFilename()
                        self.dirname = fDialog.GetDirectory()
                        f = open(os.path.join(self.dirname,self.filename),'w')
                        self.text2.SetValue(f.write())
                        f.close()
                fDialog.Destroy()

        def OnSaveAs(self, event):
                """ Save a file"""
                self.dirname = ''
                fDialog = wx.FileDialog(self, "Choose a file", self.dirname, "*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
                if fDialog.ShowModal() == wx.ID_OK:
                        self.filename = fDialog.GetFilename()
                        self.dirname = fDialog.GetDirectory()
                        f = open(os.path.join(self.dirname,self.filename),'w')
                        #textString = self.text2.SetValue(str(self.text2))
                        #f.write(self.text2.SetValue(self.text2['s']))
                        f.write(self.text2.SetValue(str(self.text2)))
                        f.close()
                fDialog.Destroy()


heres the frame method which creates the text editor:

:


class Frame(wx.Frame):
        def __init__(self, parent, id, title):
                wx.Frame.__init__(self, parent, wx.ID_ANY, title, size=(500, 500),
                                style=wx.DEFAULT_FRAME_STYLE|
                                wx.NO_FULL_REPAINT_ON_RESIZE)
                self.makemenubar()
                self.splitwin = wx.SplitterWindow(self)               
                self.text = wx.TextCtrl(self.splitwin, 1, style = wx.TE_MULTILINE)
                self.text2 = wx.TextCtrl(self.splitwin, 1, "text",  style = wx.TE_MULTILINE)
                self.splitwin.Initialize(self.text)
                self.splitwin.SetMinimumPaneSize(30)       
                self.splitwin.SplitVertically(self.text, self.text2, 0)
                self.SetPosition((50, 50))

                self.CreateStatusBar() # A StatusBar in the bottom of the window
                horizon = wx.BoxSizer(wx.HORIZONTAL)
                vert = wx.BoxSizer(wx.VERTICAL)
                vert.Add(self.text2, 1, wx.EXPAND | wx.TOP | wx.RIGHT | wx.LEFT)


the entire program file can be found here:

notesprogram.txt

Sane Nov 9th, 2006 7:12 PM

:

                        f = open(os.path.join(self.dirname,self.filename),'w')
                        self.text2.SetValue(f.write())
                        f.close()


What are you trying to do here? You write nothing to the file you just opened, then set self.text2 to the return value of f.write().

:

                        f = open(os.path.join(self.dirname,self.filename),'w')
                        f.write(self.text2.SetValue(str(self.text2)))
                        f.close()


Similarily, what are you trying to do here? You're writing to the data file the return from the method "SetValue", which is NULL. That makes no sense to me.

Presumably... you would want this instead:

:

                        f = open(os.path.join(self.dirname,self.filename),'w')
                        f.write(self.text2.GetValue())
                        f.close()


Why were you trying to set self.text2 to the value of itself anyways?

Yarvin Nov 9th, 2006 8:01 PM

okay! that does it :D thanks


All times are GMT -5. The time now is 4:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC