Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

python - Widget to Display subprocess stdout?

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:

  1. Which widget should I learn to display the stdout.
  2. How to link the stdout of subprocess to the widget you have mentioned.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You may want to use the Tkinter.Text widget, described here.

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.

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)

EDIT Have a look here for getting subprocess output line by line.

You may also want to have a look to Tkinter.Scrollbar and Text.see() to tune the display.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...