GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Sep 22, 2014 8:12 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Redirecting stdout to GtkTextView
PostPosted: Tue Dec 13, 2011 5:49 pm 
Offline

Joined: Sat Dec 10, 2011 7:13 pm
Posts: 4
Suppose I have an interface like this consisting of GtkTextView and a button.

Image

And I have code like this:

Code:
import pygtk
import gtk

class sample:
    def __init__(self):
        builder=gtk.Builder()
        builder.add_from_file("sample.xml")
        self.textview=builder.get_object("textview")
        self.ok_button=builder.get_object("ok_button")

    def on_ok(self,button,data=None):
        print "Sample Message"

if __name__ == "__main__":
    obj=sample()
    gtk.main()


Here on_ok function is the handler for the button. Now what i want is that whenever the print statement is executed, i want it's output redirected to the textview. I know that I can use GtkTextBuffer and using set_buffer() I can achieve my objective. But i want the stdout to be redirected since I'm dealing with a proprietary library of python which sends all it's output to stdout.


Top
 Profile  
 
 Post subject: Re: Redirecting stdout to GtkTextView
PostPosted: Tue Dec 13, 2011 11:37 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
You could take a look at how I'm doing it in my Django plugin for gedit (it's using GTK 3 with PyGobject, but the concepts are the same).

https://github.com/Quixotix/gedit-django-project/blob/master/djangoproject/output.py

It adds stdout to a text view as black text and stderr as red text.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject: Re: Redirecting stdout to GtkTextView
PostPosted: Wed Dec 14, 2011 11:08 am 
Offline

Joined: Sat Dec 10, 2011 7:13 pm
Posts: 4
Thanks a lot Micah Carrick.

It was exactly what I needed.

And it made my code work.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group