0
votes

I am trying to click a tkinter button to open up a new tkinter window to execute a script within it all the way up to the end with a scroll bar if necessary. However, I have only succeeded this far in getting it to run in multitude of ways in a linux window and not within a tkinter window. Can someone help me with redirecting the output of this script into the toplevel window?

self.button_run = Button(self, text="RUN", width=16, command=self.callpy)
self.button_run.grid(row=25, column=0, columnspan=2, sticky=(W + E + N + S))

def run_robbot(self):
    new = Toplevel(self)
    new.geometry('500x200')
    label = Message(new, textvariable=self.callpy, relief=RAISED)
    label.pack()

def callpy(self):
    pyprog = 'check_asim.robot'
    call(['robot', pyprog])

In the snippet above, if I pass callpy to command in Button it runs the robot script in a linux window. If I replace it to call run_robbot which is what I want and expect, it just pops up a new window with a Message Box without running the same script passed to textvariable. I have tried Enter in place of Message Box as well.

I want callpy to be executed in Toplevel tkinter window at the click of the button. How do I do it? Any tkinter operator is fine as long as it confines to the tkinter window.

1

1 Answers

0
votes

If you want to capture the output of the command, you should use subprocess.run(cmd,capture_output=True) instead. Below is an sample code:

import subprocess
from tkinter import *

class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        Button(self, text='Run', command=self.run_robot).pack()

    def run_robot(self):
        win = Toplevel(self)
        win.wm_attributes('-topmost', True)
        output = Text(win, width=80, height=20)
        output.pack()
        output.insert(END, 'Running ....')
        output.update()
        result = self.callpy()
        output.delete(1.0, END)
        output.insert(END, result)

    def callpy(self):
        pyprog = 'check_asim.robot'
        return subprocess.run(['robot', pyprog], capture_output=True).stdout

App().mainloop()