我有2个python文件。一个是helloworld.py,第二个是main.py.在main.py中有按钮。当我点击该按钮时,我想将helloworld.py的结果打印到文本框中。
helloworld.py
打印(" hello world")
所以我想将hello world字符串打印到main.py文本框
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()
答案 0 :(得分:1)
使用subprocess.check_output
代替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()