显示子进程标准输出的小部件? [英] Widget to Display subprocess stdout?
问题描述
我有一个基于 python 的 tkinter 脚本,它使用 subprocess 模块执行一些命令.我需要在 GUI 本身上显示标准输出.由于我是 tk 的新手(一周 Bold :)),我需要您的建议我应该如何进行.我有两个疑问:
I have a python based tkinter script which executes some commands using subprocess module. I need to display the standard output on the GUI itself. Since I am new to tk (One week Bold :) ), I need your advice How should i proceed. I have two queries:
- 我应该学习哪个小部件来显示标准输出.
- 如何将子流程的标准输出链接到您提到的小部件.
推荐答案
您可能想要使用 Tkinter.Text
小部件,描述为 此处.
You may want to use the Tkinter.Text
widget, described here.
描述很长,但你需要了解的主要是这个widget可以用作缓冲区,因为里面应该插入文本.
The description is quite long, but what you need to understand is mainly that this widget can be used as a buffer, as text should be inserted inside.
因此,对于 subprocess
输出中的每个新行,您必须在需要的位置插入文本.示例:
So, for each new line in the subprocess
output, you will have to insert text where you want it. Example:
t = Tkinter.Text(root)
while some_condition:
s = p.readline() # get subprocess output
t.insert(END, s)
编辑在这里查看逐行获取子进程输出.
您可能还想查看 Tkinter.Scrollbar
和 Text.see()
以调整显示.
You may also want to have a look to Tkinter.Scrollbar
and Text.see()
to tune the display.
这篇关于显示子进程标准输出的小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!