Python GUI (glade) to display output of shell process

不羁的心 提交于 2019-12-22 23:11:10

问题


I'm writing a python application that runs several subprocesses using subprocess.Popen objects. I have a glade GUI and want to display the output of these commands (running in subprocess.Popen) in the gui in real time.

Can anyone suggest a way to do this? What glade object do I need to use and how to redirect the output?


回答1:


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/




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/1929018/python-gui-glade-to-display-output-of-shell-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!