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 = savechooser.run()
        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?


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.

  • Why would I need to use Gtk+3? – TellMeWhy Mar 16 '14 at 13:01
  • I'm using gtksourceview2. – TellMeWhy Mar 16 '14 at 13:02
  • 1
    @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. – TellMeWhy Mar 16 '14 at 21:01
  • 1
    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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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