Add a method to a gtk class?

Discussion in 'Python' started by Joe Riopel, Apr 29, 2010.

  1. Joe Riopel

    Joe Riopel Guest

    On Thu, Apr 29, 2010 at 11:43 AM, Wolfnoliir <> wrote:
    > 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:


    I don't know gtk, but can you inherit from the TextBuffer class create
    your own TexBuffer subclass with the save_to_file method?
    Joe Riopel, Apr 29, 2010
    #1
    1. Advertising

  2. On 04/29/2010 10:03 AM, Wolfnoliir wrote:
    > On 29/04/2010 17:03, Joe Riopel wrote:
    >> On Thu, Apr 29, 2010 at 11:43 AM, Wolfnoliir <> wrote:
    >>> 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:

    >>
    >> I don't know gtk, but can you inherit from the TextBuffer class create
    >> your own TexBuffer subclass with the save_to_file method?

    >
    > I suppose I could but the point of adding a method is to make things
    > simpler (than having a separate function) so I don't really want to do that.


    Given that Gtk+ classes in python are often thin wrappers around opaque,
    binary Gobject-based objects, perhaps the gtk.TextBuffer cannot be
    monkey-patched in this way.
    Michael Torrie, Apr 29, 2010
    #2
    1. Advertising

  3. Joe Riopel

    Wolfnoliir Guest

    Hi,
    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
    15
    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
    Wolfnoliir, Apr 29, 2010
    #3
  4. Joe Riopel

    Wolfnoliir Guest

    On 29/04/2010 17:03, Joe Riopel wrote:
    > On Thu, Apr 29, 2010 at 11:43 AM, Wolfnoliir <> wrote:
    >> 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:

    >
    > I don't know gtk, but can you inherit from the TextBuffer class create
    > your own TexBuffer subclass with the save_to_file method?


    I suppose I could but the point of adding a method is to make things
    simpler (than having a separate function) so I don't really want to do that.
    Wolfnoliir, Apr 29, 2010
    #4
  5. Joe Riopel

    Wolfnoliir Guest

    On 29/04/2010 17:22, Michael Torrie wrote:
    > On 04/29/2010 10:03 AM, Wolfnoliir wrote:
    >> On 29/04/2010 17:03, Joe Riopel wrote:
    >>> On Thu, Apr 29, 2010 at 11:43 AM, Wolfnoliir <> wrote:
    >>>> 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:
    >>>
    >>> I don't know gtk, but can you inherit from the TextBuffer class create
    >>> your own TexBuffer subclass with the save_to_file method?

    >>
    >> I suppose I could but the point of adding a method is to make things
    >> simpler (than having a separate function) so I don't really want to do that.

    >
    > Given that Gtk+ classes in python are often thin wrappers around opaque,
    > binary Gobject-based objects, perhaps the gtk.TextBuffer cannot be
    > monkey-patched in this way.
    >
    >

    OK, I suppose I will just have to use an ordinary function then.
    Wolfnoliir, Apr 29, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,711
    Thomas Weidenfeller
    Oct 12, 2005
  2. Replies:
    2
    Views:
    508
  3. Gergely Kontra
    Replies:
    2
    Views:
    240
    Joao Pedrosa
    Jun 18, 2004
  4. Alex Shulgin
    Replies:
    1
    Views:
    169
    thefed
    Nov 21, 2007
  5. Kevin Redon

    gtk container add/remove malfunction

    Kevin Redon, Apr 21, 2009, in forum: Ruby
    Replies:
    3
    Views:
    256
    Kevin Redon
    Apr 22, 2009
Loading...

Share This Page