2017-04-07 7 views
0

私はスタックオーバーフローのいくつかのポストを読んだことがあります。Issues intercepting subprocess output in real timeRedirect command line results to a tkinter GUI、私はtkinterでスレッディングとキューを使用する必要があることを知っていますが、私はまだ同じことをすることができませんプログラムの初心者、助けてください。Tkinterスレッディングとテキストウィジェットに戻る

目標:ときにボタンを押し、Tkinterのテキストウィジェット内の「トップ」コマンドの出力とリアルタイム表示を取得

問題:私は、コードを追跡しようとしたが、それでも取得することはできません出力が、私はそれを動作させる方法を考えていない。

from tkinter import * 
import tkinter as tk 
import subprocess 
from threading import Thread 
from queue import Queue 

window = tk.Tk() 
window.title('realtime') 
window.geometry('800x400') 








text = tk.Text(window) 
text.pack() 
button = tk.Button(window, text= 'Press') 
button.pack() 


window.mainloop() 

これが唯一のGUI見通しで、

答えて

0

topが今して自分自身をリフレッシュし、私はそれはあなたがスレッドやその他もろもろをキャプチャしたい行動だ推測して助けてください。

import tkinter as tk 
from sh import top 

def update_text(): 
    text.delete(0.0, tk.END) 
    text.insert(0.0, top('-b', n=1)) 
    window.after(1000, update_text) # call this function again in 1 second 

window = tk.Tk() 
window.title('realtime') 
window.geometry('800x400') 

text = tk.Text(window) 
text.pack() 
button = tk.Button(window, text= 'Press', command=update_text) 
button.pack() 

window.mainloop() 

あなたは、私が行ったように、トップを実行するためにshをインストールする必要がある場合、またはsubprocess.check_outputを使用することがあります。ただし、この場合には、タイミングとリフレッシュを行う一度実行し、Tkinterのを持っているだけにtopを依頼する方がはるかに簡単になりますあなたは欲しい。

text.insert(0.0, subprocess.check_output(['top', '-b', '-n 1'])) 
+0

私の他の質問をお手伝いできますか?[リンク](http://stackoverflow.com/questions/43388789/use-variable-in-different-class) – Ethan

関連する問題