Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Add a method to a gtk class?

Reply
Thread Tools

Add a method to a gtk class?

 
 
Joe Riopel
Guest
Posts: n/a
 
      04-29-2010
On Thu, Apr 29, 2010 at 11:43 AM, Wolfnoliir <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
 
Michael Torrie
Guest
Posts: n/a
 
      04-29-2010
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 <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
 
Wolfnoliir
Guest
Posts: n/a
 
      04-29-2010
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

 
Reply With Quote
 
Wolfnoliir
Guest
Posts: n/a
 
      04-29-2010
On 29/04/2010 17:03, Joe Riopel wrote:
> On Thu, Apr 29, 2010 at 11:43 AM, Wolfnoliir <(E-Mail Removed)> 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.
 
Reply With Quote
 
Wolfnoliir
Guest
Posts: n/a
 
      04-29-2010
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 <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ruby Gtk, classe inheritance and initialize method problem Silkmoth Silkmoth Ruby 2 05-06-2011 04:57 PM
gtk container add/remove malfunction Kevin Redon Ruby 3 04-22-2009 10:58 AM
Ruby GTK Fortune Cookies (gtk-fortune-0.1.1) Alex Shulgin Ruby 1 11-21-2007 01:05 AM
GTK+, segmentation fault in gtK-editable_get_chars zombek@gmail.com C Programming 2 05-29-2007 03:27 PM
compiling ruby under windows (+ gtk stuff, is the gtk list dead?) Gergely Kontra Ruby 2 06-18-2004 10:20 PM



Advertisments