【发布时间】:2021-01-05 03:53:00
【问题描述】:
我正在编写一个 python 应用程序,它使用 subprocess.Popen 对象运行多个子进程。 我有一个 glade GUI,想在 gui 中实时显示这些命令的输出(在 subprocess.Popen 中运行)。
谁能建议一种方法来做到这一点?我需要使用什么林间空地对象以及如何重定向输出?
【问题讨论】:
标签: python user-interface glade
我正在编写一个 python 应用程序,它使用 subprocess.Popen 对象运行多个子进程。 我有一个 glade GUI,想在 gui 中实时显示这些命令的输出(在 subprocess.Popen 中运行)。
谁能建议一种方法来做到这一点?我需要使用什么林间空地对象以及如何重定向输出?
【问题讨论】:
标签: python user-interface glade
这是一个链接,它还显示了另一种执行此操作的方式。
我发现这很有见地,也许其他人可以使用这些技巧。
http://pygabriel.wordpress.com/2009/07/27/redirecting-the-stdout-on-a-gtk-textview/
【讨论】:
经过大量阅读并没有得到我想要的结果,我找到了另一种有效的方法。
事情是这样的
#!/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
【讨论】:
glade 只是一个用 gtk 构建 gui 的程序,所以当你要求一个 glade 对象 时,也许你应该要求 gtk 小部件,在这种情况下 textbuffer 和 textview 应该是一个解决方案,或者也许是树视图和列表存储。
subprocess.Popen 具有可以接受类文件对象的 stdout 和 stderr 参数。您可以创建一个适配器来写入文本缓冲区或在列表存储中添加项目
【讨论】: