Python 在Tkinter GUI中写入终端

Python 在Tkinter GUI中写入终端,python,tkinter,python-2.x,Python,Tkinter,Python 2.x,我试图在Tkinter GUI中实时显示命令行中的更改,我设法创建了GUI并将终端集成到其中,但我无法将按钮与终端绑定,我的代码是: import Tkinter from Tkinter import * import subprocess import os from os import system as cmd WINDOW_SIZE = "600x400" top = Tkinter.Tk() top.geometry(WINDOW_SIZE) def helloCallBack(

我试图在Tkinter GUI中实时显示命令行中的更改,我设法创建了GUI并将终端集成到其中,但我无法将按钮与终端绑定,我的代码是:

import Tkinter
from Tkinter import *
import subprocess
import os
from os import system as cmd

WINDOW_SIZE = "600x400"
top = Tkinter.Tk()
top.geometry(WINDOW_SIZE)

def helloCallBack():
   print "Below is the output from the shell script in terminal"
   subprocess.call('perl /projects/tfs/users/$USER/scripts_coverage.pl', shell=True)
def BasicCovTests():
   print "Below is the output from the shell script in terminal"
   subprocess.call('perl /projects/tfs/users/$USER/basic_coverage_tests.pl', shell=True)
def FullCovTests():
   print "Below is the output from the shell script in terminal"
   subprocess.call('perl /projects/tfs/users/$USER/basic_coverage_tests.pl', shell=True)


Scripts_coverage  = Tkinter.Button(top, text ="Scripts Coverage", command = helloCallBack)
Scripts_coverage.pack()

Basic_coverage_tests  = Tkinter.Button(top, text ="Basic Coverage Tests", command = BasicCovTests)
Basic_coverage_tests.pack()

Full_coverage_tests  = Tkinter.Button(top, text ="Full Coverage Tests", command = FullCovTests)
Full_coverage_tests.pack()

termf = Frame(top, height=100, width=500)

termf.pack(fill=BOTH, expand=YES)
wid = termf.winfo_id()

os.system('xterm -into %d -geometry 100x20 -sb &' % wid)

def send_entry_to_terminal(*args):
    """*args needed since callback may be called from no arg (button)
   or one arg (entry)
   """
    cmd("%s" % (BasicCovTests))

top.mainloop()
# 我想赢,我点击按钮,看到它在终端打印命令

至少你在正确的模块中
subprocess
还包含用于查看您运行的命令输出的实用程序,以便您可以使用perl脚本的输出

如果您只想在子进程完成运行后获得所有子进程的输出,请使用
subprocess.check\u output()
。这应该足够了

但是,如果子任务是一个长时间运行的程序,或者需要实时监控,则应该真正查看
子流程
模块中的
Popen
类。您可以创建和监视如下所示的新流程:

import subprocess

p = subprocess.Popen("perl /projects/tfs/users/$USER/scripts_coverage.pl", stdout = subprocess.PIPE, stderr = subprocess.STDOUT, shell = True)   

while True:
    line = p.stdout.readline()
    print line
    if not line: break

从那里,您可以将输出回显到终端,或者使用Tkinter小部件显示滚动程序输出。希望这能有所帮助。

您好,谢谢您的回复,您可能会错过了解我的机会,我希望看到我在python GUI中打开的终端中的perl脚本的输出是的,我想我看到了您在这里要做的事情。如果您真的想在窗口中生成终端,为什么不使用
subprocess.Popen()
而不是
os.system()
。至少这样,您可以在python程序和xterm进程之间建立链接。一旦在python程序中引用了Popen对象,您就可以使用
Popen.communicate()
与终端通信。我试图帮助您更多地思考您试图解决的问题。谢谢您的帮助,我不是python专家,您能给我一个示例吗?如何使用/:subprocess.Popen()而不是os.system(),以及如何使用Popen.communicate()在终端中显示输出。