[Tutor] How to save text from a wxTextCtrl widget [was: Tutor Digest, Vol 32, Issue 70]

Luke Paireepinart rabidpoobear at gmail.com
Tue Oct 17 08:13:51 CEST 2006

Pine Marten wrote:
>> Did you create this code, or did someone give it to you?  It looks
>> like it's been made with BoaConstructor.  If you're using Boa for
>> development, you'd probably be better off using it to add your save
>> functionality, rather than writing the code by hand.
>> Unfortunately, I can't help you with that, but maybe someone else can.
>> In fact, I see you've already got Save and SaveAs buttons, so maybe
>> you just need to edit OnSaveButton() and OnSaveAsButton().
> I used Boa Constructor to make it.  As far as I understand it, Boa sets 
> things up for you but then you have to add code to complete things, it does 
> not have an option to add save functionality on its own.
> In this case I did in fact try to add the code you originally suggested 
> under the OnSaveButton() and OnSaveAsButton() but was unable to get it to 
> work.
> Teetering on the brink of just bagging trying to learn Python, but thanks 
> for your suggestions.
I'd hate for you to give up learning Python just cause of one problem!
Here, I'll take you through the example code that John wrote.

Suppose you've got a TextCtrl:

#here's our textctrl.
myTextBox = wx.TextCtrl(parent, style=wx.TE_MULTILINE)

You can save it simply like this:

#a function that takes an event as an argument (note that it doesn't actually use this event)
def save(event):
    savefile = open(self.filename, 'w')#open the file (self.filename) to store our saved data
    savefile.write(myTextBox.GetValue())#get our text from the textctrl, and write it out to the file we just opened.
    savefile.close()#and then close the file.

saveButton = wx.Button(parent, id=wx.ID_SAVE)#we make a button
saveButton.Bind(wx.EVT_BUTTON, save)#and we bind it to the 'save' function
#so whenever someone clicks on the button it will call the 'save' function with an argument (event).

#all this other stuff isn't necessary to the saving function, it's just
#if you want to give the users some kind of dialog to enter
#where to save the file.  If you're going to save the text to a certain
#file in all cases, then you wouldn't need to do any of this.
#so for now, let's just skip this part.
Of course, you'll need some way of setting self.filename.  The usual
way would be to have a Save button and a Save as.. button, and have
Save as.. invoke a wx.FileDialog.

Something like:

def saveas(event):
    dialog = wx.FileDialog(parent, message='Choose a file',
    if dialog.ShowModal() == wx.ID_OK:
        self.savefile = dialog.GetFilename()
saveAsButton = wx.Button(parent, id=wx.ID_SAVEAS)
saveAsButton.Bind(wx.EVT_BUTTON, saveas)

You could also modify save() to automatically call saveas() if
self.filename is not set.
(just be careful of recursion if the user clicks cancel!)

Loading is the same except in the opposite direction.  Look at
wx.TextCtrl.SetValue and the style options for wx.FileDialog.


Okay, now that we know what we want to use, let's put it into your code!

#Don't need to change any of this.
# I'll leave any relevant code.
import wx

[snip code]

class Frame1(wx.Frame):
[snip code]
        self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1, 
              parent=self.panel1, pos=wx.Point(40, 56), size=wx.Size(264, 
              style=wx.TE_MULTILINE, value='textCtrl1')

        self.Save = wx.Button(id=wxID_FRAME1SAVE, label='Save', name='Save',
              parent=self.panel1, pos=wx.Point(152, 32), size=wx.Size(75, 
        self.Save.Bind(wx.EVT_BUTTON, self.OnSaveButton, id=wxID_FRAME1SAVE)

[snip code]
#Okay, here is the function we need to change.

    def OnSaveButton(self, event):
        #from John's save(event) function:
        #savefile = open(self.filename, 'w')
        savefile = open('test.txt','w')
        #if you don't want 'test.txt' try using a variable.

        #textCtrl1 is an attribute of your Frame1 class.

        #then we just save it.

Do you understand which parts of John's examples were changed and why?
Hope That Helps,

More information about the Tutor mailing list