2

I am trying to show the changes in the command line in real time in my Tkinter GUI, I managed to create the GUI and integrate the terminal into it, but I cant bind the buttons with the terminal, my code is :

import Tkinter
from Tkinter import *
import subprocess
import os
from os import system as cmd

WINDOW_SIZE = "600x400"
top = Tkinter.Tk()
top.geometry(WINDOW_SIZE)

def helloCallBack():
   print "Below is the output from the shell script in terminal"
   subprocess.call('perl /projects/tfs/users/$USER/scripts_coverage.pl', shell=True)
def BasicCovTests():
   print "Below is the output from the shell script in terminal"
   subprocess.call('perl /projects/tfs/users/$USER/basic_coverage_tests.pl', shell=True)
def FullCovTests():
   print "Below is the output from the shell script in terminal"
   subprocess.call('perl /projects/tfs/users/$USER/basic_coverage_tests.pl', shell=True)


Scripts_coverage  = Tkinter.Button(top, text ="Scripts Coverage", command = helloCallBack)
Scripts_coverage.pack()

Basic_coverage_tests  = Tkinter.Button(top, text ="Basic Coverage Tests", command = BasicCovTests)
Basic_coverage_tests.pack()

Full_coverage_tests  = Tkinter.Button(top, text ="Full Coverage Tests", command = FullCovTests)
Full_coverage_tests.pack()

termf = Frame(top, height=100, width=500)

termf.pack(fill=BOTH, expand=YES)
wid = termf.winfo_id()

os.system('xterm -into %d -geometry 100x20 -sb &' % wid)

def send_entry_to_terminal(*args):
    """*args needed since callback may be called from no arg (button)
   or one arg (entry)
   """
    cmd("%s" % (BasicCovTests))

top.mainloop()
#

I want win I click the button to see it printing the command in the terminal enter image description here

Ren
  • 2,852
  • 2
  • 23
  • 45
user1941008
  • 373
  • 3
  • 6
  • 13

1 Answers1

0

Well you are in the right module at least. subprocess also contains utilities for viewing the output of the command that you run, so that you can have the output of your perl script made available to you.

If you want to simply get all of the subprocesses output after it finishes running, use subrocess.check_output(). It should be more than sufficient.

However, if the subtask is a long running program or you require monitoring in real-time, you should really look at the Popen class in the subprocess module. You can create and monitor a new process like this:

import subprocess

p = subprocess.Popen("perl /projects/tfs/users/$USER/scripts_coverage.pl", stdout = subprocess.PIPE, stderr = subprocess.STDOUT, shell = True)   

while True:
    line = p.stdout.readline()
    print line
    if not line: break

From there you could echo the output into a terminal or use a Tkinter widget to display the rolling program output. Hope this helps.

pholtz
  • 464
  • 3
  • 10
  • Hi, Thanks for your reply , you might miss understand me, I would like to see the output of the perl script inside the terminal that I have opened in the python GUI – user1941008 Mar 31 '16 at 13:00
  • Yes I think I see what you're trying to do here. Well if you really want to spawn a terminal inside your window, why not use `subprocess.Popen()` instead of `os.system()`. At least that way you would have a link between your python program and the xterm process. – pholtz Mar 31 '16 at 13:09
  • Once you have the Popen object referenced in your python program, you could communicate with the terminal using `Popen.communicate()`. I'm trying to help you think more about the problem you're trying to solve here. – pholtz Mar 31 '16 at 13:11
  • Thanks for your help, I am not expert at python, can you give me an example how do I do that ? How do I use the /: subprocess.Popen() Instead of os.system() and how do i use the Popen.communicate() to show the ouput in the terminal. – user1941008 Mar 31 '16 at 13:22