Reputation: 159
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()
Upvotes: 0
Views: 1506
Reputation: 16169
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()
Upvotes: 1