0

I have 2 python files. one is helloworld.py and 2nd is main.py. In main.py there is button.When i click on that button that time I want to print result of helloworld.py into text box.

helloworld.py

print("hello world")

so I want to print hello world string into main.py textbox

from tkinter import *
import os
root= Tk()
root.title("My First GUI")
root.geometry("800x200")
frame1=Frame(root)
frame1.grid()

def helloCallBack():
     result = os.system('python helloworld.py')
     if result==0:
         print("OK")
         text1.insert(END,result)        
     else:
         print("File Not Found")

label1 = Label(frame1, text = "Here is a label!")
label1.grid()

button1 = Button(frame1,text="Click Here" , foreground="blue",command= helloCallBack)
button1.grid()

text1 = Text(frame1, width = 35, height = 5,borderwidth=2)
text1.grid()

radiobutton1 = Radiobutton(frame1, text= "C Programming", value=0)
radiobutton1.grid()
radiobutton2 =Radiobutton(frame1, text= "Python Programming")
radiobutton2.grid()
root.mainloop()
j_4321
  • 15,431
  • 3
  • 34
  • 61
Ashish Aware
  • 169
  • 2
  • 15

1 Answers1

1

Use subprocess.check_output instead of os.system:

from tkinter import *
from subprocess import check_output, CalledProcessError

root = Tk()

text1 = Text(root)
text1.pack()

def command():
    try:
        res = check_output(['python', 'helloworld.py'])
        text1.insert(END, res.decode())
    except CalledProcessError:
        print("File not found")

Button(root, text="Hello", command=command).pack()

root.mainloop()
j_4321
  • 15,431
  • 3
  • 34
  • 61
  • Thank you its working. But, Now I am getting the whole output at once. What I want is that the GUI should print the output iteratively just like it prints on terminal. – Ashish Aware Nov 24 '16 at 12:02
  • I don't get what you mean by iteratively, I guess that you think about something more complicated than just printing `hello world`. Could you give me an example? – j_4321 Nov 24 '16 at 12:08
  • I have flash.py python file through that i get images from Flashair. for that want to iterate loop – Ashish Aware Nov 24 '16 at 12:15
  • Check this question: http://stackoverflow.com/questions/2804543/read-subprocess-stdout-line-by-line – j_4321 Nov 24 '16 at 16:08