Python GUI (glade) to display output of shell process

你离开我真会死。 提交于 2019-12-06 10:39:30

Here is a link that also displays another way of doing this.

I found this to be very insightful, maybe someone else can use these tips.

http://pygabriel.wordpress.com/2009/07/27/redirecting-the-stdout-on-a-gtk-textview/

glade is only a program to build gui with gtk so when you ask for a glade object maybe you should ask for gtk widget and in this case textbuffer and textview chould be a solution or maybe treeview and liststore. subprocess.Popen has stdout and stderr arguments that can accept a file-like object. you can create an adapter that writes to the textbuffer or add items in the liststore

After lots of reading and not getting the results I wanted, I found another method that works.

It goes like this

#!/usr/bine/env python
import subprocess
import gtk

### Of course, you should have the gui built and know which widgets to use for this.
viewer = self.builder.get_widget('txtview')
proc = subprocess.Popen('ls -al /home'.split(), stdout=subprocess.PIPE, stderr = subprocess.STDOUT)
while True:
    line = proc.stdout.readline()
    viewer.get_buffer().instert_at_cursor(line)
    if not line:
       break
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!