2

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

谁能建议一种方法来做到这一点?我需要使用什么林间空地对象以及如何重定向输出?

4

3 回答 3

2

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/

于 2009-12-21T20:56:58.020 回答
2

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

它是这样的

#!/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
于 2009-12-21T16:17:48.420 回答
1

glade 只是一个用 gtk 构建 gui 的程序,所以当你要求一个glade 对象时,也许你应该要求gtk 小部件,在这种情况下,textbuffer 和 textview 应该是一个解决方案,或者可能是 treeview 和 liststore。 subprocess.Popenhasstdoutstderrarguments 可以接受类似文件的对象。您可以创建一个写入文本缓冲区的适配器或在列表存储中添加项目

于 2009-12-18T16:12:15.587 回答