Add a method to a gtk class?

Wolfnoliir wolfnoliir at
Thu Apr 29 17:43:16 CEST 2010

I would like to add a method to the gtk.TextBuffer class to save a text
buffer to a file, but I get an error:

line 22, in <module>
    gtk.TextBuffer.save_to_file = gtk_TextBuffer_save_to_file
TypeError: can't set attributes of built-in/extension type 'gtk.TextBuffer'

Here is the code:

10 import gtk
16 def gtk_TextBuffer_save_to_file(self, filePath) :
17     with open(filePath, 'w') as file:
18         start = self.get_start_iter()
19         end   = self.get_end_iter()
20         text  = self.get_text(start, end, False)
21         file.write(text)
22 gtk.TextBuffer.save_to_file = gtk_TextBuffer_save_to_file

More information about the Python-list mailing list