在 Tkinter GUI 中写入终端 [英] Write to terminal in Tkinter GUI

查看:31
本文介绍了在 Tkinter GUI 中写入终端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to show the changes in the command line in real time in my Tkinter GUI, I managed to create the GUI and integrate the terminal into it, but I cant bind the buttons with the terminal, my code is :

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

#

我想赢我点击按钮看到它在终端中打印命令

#

I want win I click the button to see it printing the command in the terminal

推荐答案

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

Well you are in the right module at least. subprocess also contains utilities for viewing the output of the command that you run, so that you can have the output of your perl script made available to you.

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

If you want to simply get all of the subprocesses output after it finishes running, use subrocess.check_output(). It should be more than sufficient.

然而,如果子任务是一个长时间运行的程序或者你需要实时监控,你真的应该看看subprocess模块中的Popen类.您可以像这样创建和监控新进程:

However, if the subtask is a long running program or you require monitoring in real-time, you should really look at the Popen class in the subprocess module. You can create and monitor a new process like this:

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

From there you could echo the output into a terminal or use a Tkinter widget to display the rolling program output. Hope this helps.

这篇关于在 Tkinter GUI 中写入终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆