Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to create a text editor for Ubuntu (going to give it a try!) using gtk2 and python. So far I have been able to implement the open function. However I just don't know how I can get the contents of the TextView and write them into a file (the user would have named and decided which directory to save it in).

Here is the code for the gtk.FileChooserDialog save function:

def on_saveButton_clicked(widget):
        print 'saveButton clicked'
        savechooser = gtk.FileChooserDialog(title='Save File', action=gtk.FILE_CHOOSER_ACTION_SAVE, 
                                                        buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                                        gtk.STOCK_SAVE, gtk.RESPONSE_OK))
        filter = gtk.FileFilter()
        filter.set_name('All files')

        pyFilter = gtk.FileFilter()
        pyFilter.set_name('Python source file')

        rbFilter = gtk.FileFilter()
        rbFilter.set_name('Ruby source file')

        cppFilter = gtk.FileFilter()
        cppFilter.set_name('C++ source file')

        response =
        if response == gtk.RESPONSE_OK:
            filename = savechooser.get_filename()
            print filename, 'selected.'
        elif response == gtk.RESPONSE_CANCEL:
            print 'Closed, file not saved.'

Does anyone have any ideas?

share|improve this question
I'd start this kind of project by using GTK3 instead, look at this tutorial for example: – Sylvain Pineau Mar 15 '14 at 22:34
up vote 2 down vote accepted

In the first place, consider updating your code to Python 3 and Gtk+3 (PyGObject) as @SylvainPineau suggests. Then, you need to make the GtkTextView an instance property/member so you can access it from within on_saveButton_clicked. Lastly, to actually save the contents of the GtkTextBuffer you can use Python builtin functions or GIO.

With Python builtin functions (and assuming you save the GtkTextView in self.view):

if response == gtk.RESPONSE_OK:
    filename = savechooser.get_filename()
    print(filename, 'selected.')

    buf = self.view.get_buffer()
    text = buf.get_text(buf.get_start_iter(),
        open(filename, 'w').write(text)
    except SomeError as err:
        print('Could not save %s: %s' % (filename, err))

PS: I notice you are not using classes, since your on_saveButton_clicked function doesn't have the self argument. If so, for the sake of testing your current code you can make self.view into a TEXT_VIEW global variable.

share|improve this answer
Why would I need to use Gtk+3? – DevRobot Mar 16 '14 at 13:01
I'm using gtksourceview2. – DevRobot Mar 16 '14 at 13:02
@Abunujum, well, you don't have to use Gtk+3. It's recommended nonetheless, that way your application will be able to take advantage of the new features, improvements and bug fixes :) – edwin Mar 16 '14 at 18:49
Yes ... that may be a better idea, it will be useful in the long run I suppose. – DevRobot Mar 16 '14 at 21:01
In that line, I am creating a file object with write mode (because I am going to write to the file). In general, in the Python interpreter you can type help(thing) to get help about thing. Try running python in a terminal, then execute help(open) to check it out. – edwin Mar 17 '14 at 23:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.