1

I have done 2 separate things and now I want to join the two. I am using command prompt on Windows 8

  1. I have a TCL file that I can run from python. It gives all the outputs on command prompt. I use this command to run the TCL

    os.system(r'vivado -mode tcl -source location/my_tcl.tcl')

full code:

import Tkinter as tk
import os
import time

my_gui = tk.Tk()
mvar = tk.StringVar()

def my_hello():
    chk = mvar.get()
    if chk == '1':
        os.system(r'vivado -mode tcl -source path/my_tcl.tcl')
        f = open('input.txt', 'r')
        a = f.readline().rstrip('\n')
        if a == 'Passed':
            mlabel = tk.Label(my_gui,text = 'Test Passed', fg = 'black', bg = 'green',width = 10).place(x=200,y=10)
        else:
            mlabel = tk.Label(my_gui,text = 'Test Failed', fg = 'black', bg = 'red',width = 10).place(x=200,y=10)
        f.close
    else:
        mlabel = tk.Label(my_gui,text = 'No Test Run', fg = 'black', bg = 'yellow',width = 10).place(x=200,y=10)

my_gui.geometry('300x300+200+200')
my_gui.title('Test')

mbutton = tk.Button(my_gui,text = 'Run Tests!',command = my_hello).place(x=150,y=280,anchor= 'center')

check1 = tk.Checkbutton(my_gui,text = 'DDR Test',state = 'active', variable = mvar).place(x=10,y=10)
mvar.set('0')


my_gui.mainloop()
  1. I have implemented simple logging on python following solutions from this thread: Python Logging to Tkinter Text Widget

full code: https://gist.github.com/bitsgalore/901d0abe4b874b483df3ddc4168754aa

Now I want to combine the two parts but logging require some sort of string input in order to store and display log. while output of TCL appears on command prompt. What i want to achieve is that all that appears on command prompt can be visible on my Tkinter text widget.

Is there a way I can do this? Regards

2
  • show some of the code , then we can tell something Commented Sep 6, 2017 at 12:43
  • @AkshayKathpal I have added the code but as I said, they are 2 different tasks and I am not sure how to join them Commented Sep 6, 2017 at 14:26

1 Answer 1

2

Here is a somewhat static approach. You can use the subprocess module, which allows you to capture the process' output, and then display it in a tkinter.Text widget.

import subprocess

def run_process():
    path = "script_with_some_output"
    p = subprocess.run(path, stdout=subprocess.PIPE)

    return p.stdout

Now, you can call run_process() to execute your process, and get its output as a sequence of bytes. You can easily log that into a Text widget, by calling text.insert(tk.END, run_process()).

Here is a short example demonstrating this:

import tkinter as tk
import sys
import subprocess

def run_process():
    path = r"path/to/script"
    p = subprocess.run("python"+path, stdout=subprocess.PIPE)
    return p.stdout

root = tk.Tk()
log = tk.Text(root)
b = tk.Button(root, text="Run stuff", command=lambda: log.insert(tk.END, run_process()))

log.pack()
b.pack()    

root.mainloop()

This script will run the script located at the given path, and display its output in the text widget.

Sign up to request clarification or add additional context in comments.

4 Comments

I have already run a python script and captured it's stdout to the text widget but in some other way. I will try this method as well and get back to you. still i am not sure if this will work for my TCL script.
I ended up doing it a very different way but your solution was good too. Only thing missing is that it cannot display in parallel. It display complete output once the whole script completes
@MohsinShafiq Yes, I tried and fiddled a while with file descriptors in order to redirect stdout to the widget in real time, but I couldn't find any solution... how did you end up doing it? Would you mind posting it as an answer?
The script that i was running already making a log for all the output into a .log file. I used solution provided here: gist.github.com/bitsgalore/901d0abe4b874b483df3ddc4168754aa This code makes a separate thread to update contents of text widget in run time. I added another thread which calls my script. script was creating a text file. So the other thread was responsible of reading the .log file and putting it line by line into the text widget. run the code first and make changes in order to make it according to your need

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.