Python 与GUI(Tkinter)交互的subprocess.call()
有没有办法使用Python 与GUI(Tkinter)交互的subprocess.call(),python,python-2.7,tkinter,subprocess,Python,Python 2.7,Tkinter,Subprocess,有没有办法使用subprocess.call()或subprocess.Popen()并通过Tkinter的条目小部件与stdin交互,并将stdout输出到文本小部件 我真的不知道如何处理这样的事情,因为我不熟悉使用子流程模块。我想我已经掌握了将条目作为子流程标准输入的基本知识。您可能需要根据自己的需要调整它(re:output toTextwidget) 此示例调用一个测试脚本: # test.py: #!/usr/bin/env python a = raw_input('Type so
subprocess.call()
或subprocess.Popen()
并通过Tkinter的条目
小部件与stdin交互,并将stdout输出到文本
小部件
我真的不知道如何处理这样的事情,因为我不熟悉使用
子流程
模块。我想我已经掌握了将条目
作为子流程标准输入的基本知识。您可能需要根据自己的需要调整它(re:output toText
widget)
此示例调用一个测试脚本:
# test.py:
#!/usr/bin/env python
a = raw_input('Type something!: \n') #the '\n' flushes the prompt
print a
这只需要一些输入(来自sys.stdin
)并将其打印出来
通过GUI调用并与之交互是通过以下方式完成的:
from Tkinter import *
import subprocess
root = Tk()
e = Entry(root)
e.grid()
b = Button(root,text='QUIT',command=root.quit)
b.grid()
def entryreturn(event):
proc.stdin.write(e.get()+'\n') # the '\n' is important to flush stdin
e.delete(0,END)
# when you press Return in Entry, use this as stdin
# and remove it
e.bind("<Return>", entryreturn)
proc = subprocess.Popen('./test.py',stdin=subprocess.PIPE)
root.mainloop()
但我建议阅读其他一些人们已经做到这一点的例子。哇哈哈,我的一个问题第一次连一天的评论都没提;p关于将stdout重定向到Tkinter小部件,在SO和其他地方(例如)有很多答案。但是,如果有人碰巧知道的话,我还想知道如何从GUI小部件将stdin传递给子流程!是的,我想知道如何让STDIN工作(那将是一个很好的!xD),但是对于链接:)用Python对象替换
sys.stdout
不会影响子进程的stdout,请参阅。要在GUI小部件中显示子流程的标准输出,请参阅
class MyStdout(object):
def __init__(self,textwidget):
self.textwidget = textwidget
def write(self,txt):
self.textwidget.insert(END,txt)
sys.stdout = MyStdout(mytextwidget)