在Tkinter GUI中写入终端

时间:2016-03-31 12:18:39

标签: python tkinter python-2.x

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

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()

我想获胜我点击按钮看它在终端上打印命令 enter image description here

1 个答案:

答案 0 :(得分:0)

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

如果您想在完成运行后简单地获取所有子进程输出,请使用subrocess.check_output()。应该绰绰有余。

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

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小部件来显示滚动程序输出。希望这会有所帮助。

相关问题