2016-03-31 66 views
1

Tkinter GUIでコマンドラインの変更をリアルタイムで表示しようとしていますが、GUIを作成してターミナルを組み込むことができましたが、ターミナルでボタンをバインドできません、私のコードは次のとおりです。Tkinter 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() 

私はさてあなたは、少なくとも右のモジュールであり、それは、端末 enter image description here

答えて

0

でコマンドを印刷見るために、私はボタンをクリック勝ちたいです。 subprocessには、実行するコマンドの出力を表示するためのユーティリティも含まれているので、perlスクリプトの出力を利用できるようになります。

実行終了後に出力されるすべてのサブプロセスを取得する場合は、subrocess.check_output()を使用します。それ以上のものでなければなりません。

サブタスクが長時間実行中のプログラムである場合、またはリアルタイムで監視する必要がある場合は、subprocessモジュールの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のウィジェットを使用することができます。お役に立てれば。

+0

こんにちは、 あなたの返事をお寄せいただきありがとうございます。私はPython GUIで開いた端末内のperlスクリプトの出力を見たいと思います – user1941008

+0

はい、私はあなたが何をしようとしていると思うここで行うこと。ウィンドウ内に端末を生成したいのであれば、 'os.system()'の代わりに 'subprocess.Popen()'を使用してみてください。少なくとも、あなたのpythonプログラムとxtermプロセスの間にはリンクがあります。 – pholtz

+0

PythonプログラムでPopenオブジェクトが参照されると、 'Popen.communicate()'を使って端末と通信することができます。私はあなたがここで解決しようとしている問題についてもっと考えるのを手伝っています。 – pholtz

関連する問題