Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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')
        filter.add_pattern('*')
        savechooser.add_filter(filter)

        pyFilter = gtk.FileFilter()
        pyFilter.set_name('Python source file')
        pyFilter.add_pattern('*.py')
        savechooser.add_filter(pyFilter)

        rbFilter = gtk.FileFilter()
        rbFilter.set_name('Ruby source file')
        rbFilter.add_pattern('*.rb')
        savechooser.add_filter(rbFilter)

        cppFilter = gtk.FileFilter()
        cppFilter.set_name('C++ source file')
        cppFilter.add_pattern('*.cpp')
        savechooser.add_filter(cppFilter)

        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.'
        savechooser.destroy()

Does anyone have any ideas?

share|improve this question
2  
I'd start this kind of project by using GTK3 instead, look at this tutorial for example: python-gtk-3-tutorial.readthedocs.org/en/latest/textview.html –  Sylvain Pineau Mar 15 at 22:34

1 Answer 1

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(),
                        buf.get_end_iter(),
                        True)
    try:
        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 at 13:01
    
I'm using gtksourceview2. –  DevRobot Mar 16 at 13:02
    
To use Gtk+3, I'd have to rewrite it all over again! * moan * –  DevRobot Mar 16 at 13:03
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 at 18:49
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 at 23:31

Your Answer

 
discard

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.