简体   繁体   中英

PyGTK and Glade 3 - How to get the output of 'print' to TextView

I'm quite new in programming, even in speaking in English.. so I'm just trying to explain my problem here:

I'm building a GUI using Glade 3, which need show all output of 'print' in a TextView or some Widgets else of Glade 3,

My python code is something like this:

#ab.py
def a():
    print 'AAAAA'

def b():
    print 'BBBBB'

and

#button_c.py
def button_c():
    if:
      print '11111'
    elif:
      print '22222'
    else:
      print '33333'

    a()
    b()

if __name__ == '__main__':

    button_c()

I'm trying use some code like this to show all of the output of 'print' in the object textview1 in a real time, just like the Python Interpreter did:

    def on_button_c_clicked(self, widget):
        button_c()
        self.builder.get_object("textview1").get_buffer().set_text(??????)

I've googled a lot and seeing something about sys.stdout, thread, subprocess and textbuffer, but I still can't figure it out, the only idea i have in the moment is change all 'print' parte of the code as:

    def a():
        # print 'AAAAA'
        output_gui = 'AAAAA'
        print output_gui
        self.builder.get_object("textview1").get_buffer().set_text(output_gui)

I haven't tried this yet because there must be another smarter way, no?

You can look at Codimension (python IDE) sources. There is one of solutions. All sources written with python.

More close:

if .deb package (I have Debian linux) installed - /usr/share/codimension/ui/mainwindow.py, class CodimensionMainWindow, __ init__(), or in the same file from tarball. It uses Redirector (self-written, I think) module (very small) also - /usr/share/codimension/ui/redirector.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM