Mohsin Shafiq
Mohsin Shafiq

Reputation: 25

Logging output of TCL script into Tkinter text widget

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

Upvotes: 1

Views: 896

Answers (1)

Right leg
Right leg

Reputation: 16730

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.

Upvotes: 2

Related Questions