3

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.
Abhranil Das
  • 5,702
  • 6
  • 35
  • 42
Ani
  • 918
  • 1
  • 10
  • 25

1 Answers1

2

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.

Community
  • 1
  • 1
Joël
  • 2,723
  • 18
  • 36
  • 1
    While that's the _outline_ of what's needed, you want to keep the GUI live during the processing. That makes things quite a lot more complex (though not as much as if you want to interact with the subprocess; there are many subtleties). – Donal Fellows Nov 15 '11 at 13:50
  • That's right, keeping activity is a bit different, as GUI has to be refreshed. But this is more application-dependent: is the GUI the root of the program, or just a display? This will have two different answers, using `root.after()` or access to `t` properties. – Joël Nov 15 '11 at 15:37
  • Hey guys, thanks for your replies. I will look in to the links and will get back to you. Actually I have a huge csh script which is quite complex to use. With GUI, I am trying to isolate user from the complexity. Hence it is essential to give user real time of csh script execution. – Ani Nov 16 '11 at 03:40
  • I had yesterday tried similar implementation as you suggested, the only issue was that I was getting stdout in text box only once the execution was finished in console. – Ani Nov 16 '11 at 03:55
  • Is the answer given in the question linked in my first edit working? Second question, could you please publish the code you have so far? There's a little trick to make with GUI, since they are not procedural but event-driven (I hope I have the correct terms). – Joël Nov 16 '11 at 07:41