Python GUI (glade) 显示 shell 进程的输出 [英] Python GUI (glade) to display output of shell process

查看:32
本文介绍了Python GUI (glade) 显示 shell 进程的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 python 应用程序,它使用 subprocess.Popen 对象运行多个子进程.我有一个空地 GUI,想在 gui 中实时显示这些命令的输出(在 subprocess.Popen 中运行).

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?

推荐答案

经过大量阅读但没有得到我想要的结果后,我找到了另一种有效的方法.

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

事情是这样的

#!/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_object('txtview')
proc = subprocess.Popen('ls -al /home'.split(), stdout=subprocess.PIPE, stderr = subprocess.STDOUT)
while True:
    line = proc.stdout.readline()
    viewer.get_buffer().insert_at_cursor(line)
    if not line:
       break

这篇关于Python GUI (glade) 显示 shell 进程的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆