I have tried using several different similar solutions that I have found online, but none seem to quite do what I am aiming for.
I want to call an external script (helloworld.py) into my tkinter gui. I want this called script (helloworld.py) to execute all the functions that are contained in it upon a button press in the gui and print the resulting outputs into the gui, not the console. I have found some solutions which will print the output to the console, but I am unable to get it to display in the gui. Any solutions that I have found that print to the gui do not work when I try to get the output to come from a called external script.
I appreciate any help. I am definitely a novice, so I apologize for what is probably a basic question and the inability to connect the dots for myself on similar questions asked on here. Below is one of the versions of code that I am currently working with. Thank you in advance for your help!
import Tkinter
import sys
import subprocess
sys.path.append('/users/cmbp')
def callback():
import os
print subprocess.call('python /users/cmbp/p4e/helloworld.py',
shell=True)
lbl = Tkinter.Label(master)
lbl.pack()
master = Tkinter.Tk()
master.geometry('200x90')
master.title('Input Test')
Btn1 = Tkinter.Button(master, text="Input", command=callback)
Btn1.pack()
master.mainloop()
EDIT
I also started having some success with trying to import the called script as a module. The problem with this is I can only get one function to print out from the called script even though there are multiple functions that I want to try and call (I just want the entire called script to print out all the results of its functions).
Here is an example of a script that I want to call helloworld.py:
def cooz():
return ('hello worldz!')
def tooz():
return ("here is another line")
def main():
return cooz()
return tooz()
And here is an example of the tkinter gui script that is trying to import helloworld.py:
import Tkinter as tk
import helloworld
def printSomething():
y = helloworld.main()
label = tk.Label(root, text= str(y))
label.pack()
root = tk.Tk()
root.geometry('500x200')
root.title('Input Test')
button = tk.Button(root, text="Print Me", command=printSomething)
button.pack()
root.mainloop()
This results in only the first function printing ('hello worldz!'). Any thoughts on why it only will return one line and not the entire helloworld.py script?
freopen? That's a C function but I think it would be applicable for you as well.stdoutis a standard operation in consoles, IDEs, and other software. So no need to detect singular "print statements". I wouldn't know where to start for Python, hence my suggestion.errorwindow.pyin my answer to another question which shows how to redirect output sent tostdoutandstderrto atkinterwindow. What it does is very similar to what you want to do (and it doesn't require—and isn't specific to—using theeasyguimodule).subprocesshas other functions to get output - ie. check_output()