2017-08-04 6 views
0

私はloginfoでテキスト領域を連続的に更新する方法を理解しようとしています。私は、次の(私のコードから抜粋)pyqtの `less + F logfile`

import sys 
import os 
import PyQt4.QtGui as gui 
import PyQt4.QtCore as core 

class ApplicationWindow(gui.QMainWindow): 
    ''' 
    Our main application window 
    ''' 
    def __init__(self): 
     gui.QMainWindow.__init__(self) 
     self.main_widget = gui.QWidget(self) 
     l = gui.QHBoxLayout(self.main_widget) 

     self.te = gui.QPlainTextEdit() 
     self.rdock = gui.QDockWidget("Output",self) 
     self.rdock.setWidget(self.te) 
     self.te.setReadOnly(True) 
     self.addDockWidget(core.Qt.RightDockWidgetArea,self.rdock) 

     self.run_command("less +F test.txt") 
     self.main_widget.setFocus() 
     self.setCentralWidget(self.main_widget) 

    def run_command(self,fcmd): 
     ''' 
     Runs our desired command and puts the output into the right dock 
     ''' 
     cmd = str(fcmd) 
     stdouterr = os.popen4(cmd)[1].read() 
     self.te.setPlainText(stdouterr) 

qApp = gui.QApplication(sys.argv) 
aw = ApplicationWindow() 
aw.setWindowTitle("%s" %progname) 
aw.show() 
sys.exit(qApp.exec_()) 

ここに私の問題は、プログラムがハングアップするということですがあります。私は出力を継続的に表示できるようにしたいと思うし、最終的にはそのコマンドをkillしてless +F otherFile.txtを実行したいと思うでしょう。私はlessコマンドを使用することに専念していない、私はちょうどファイルの連続した末尾を見たい。

私はこのように、スレッドを使用して試してみましたが、無駄に

runThread = threading.Thread(target=self.run_command("less +F test.txt")) 
runThread.daemon = True 
runThread.start() 

は、私はメインをブロックしていないんだように、私は別のスレッドでのostreamコマンドを実行する必要がある印象を取得していますしかし、これを行うにはどうすればよいかわかりません。

答えて

1

スレッドを使用することはオプションですが、あなたのケースでは最適ではありませんが、GUIを使用する方が使いやすいのでタイマーを使用することをお勧めします。

def run_command(self): 
    ''' 
    Runs our desired command and puts the output into the right dock 
    ''' 
    stdouterr = os.popen4(self.cmd)[1].read() 
    self.te.setPlainText(stdouterr) 

をそしてあなただけの自己への新しい文字列を渡すコマンドを変更するには:

timer = core.QTimer(self) 
timer.timeout.connect(lambda: self.run_command("less +F test.txt")) 
timer.start(10) # milliseconds 

はまた、あなたが、私はあなたが以下にごrun_command機能を変更するお勧めします、コマンドを変更したいことをコメントしています。 CMD:

self.cmd = "less +F otherFile.txt" 

完全な例:

class ApplicationWindow(gui.QMainWindow): 
    ''' 
    Our main application window 
    ''' 
    def __init__(self): 
     gui.QMainWindow.__init__(self) 
     self.main_widget = gui.QWidget(self) 
     l = gui.QHBoxLayout(self.main_widget) 

     self.te = gui.QPlainTextEdit() 
     self.rdock = gui.QDockWidget("Output",self) 
     self.rdock.setWidget(self.te) 
     self.te.setReadOnly(True) 
     self.addDockWidget(core.Qt.RightDockWidgetArea,self.rdock) 

     self.main_widget.setFocus() 
     self.setCentralWidget(self.main_widget) 

     self.cmd = "less +F test.txt" 
     timer = core.QTimer(self) 
     timer.timeout.connect(self.run_command) 
     timer.start(10) 

    def run_command(self): 
     ''' 
     Runs our desired command and puts the output into the right dock 
     ''' 
     stdouterr = os.popen4(self.cmd)[1].read() 
     self.te.setPlainText(stdouterr) 
+0

これはほぼ完璧です。私はちょうど実際のユースケースで2つの問題があります。最初は 'test.txt'でテキストが積極的に生成されていますが、ウィンドウは更新されません(' less + F'と同じように)。もう一つは、私の出力にはテールエンドのみを表示したいのです。これは常に先頭から始まり、ファイル全体を表示します。 –

+0

最初の点はうまくいきます。テストを行っている様子、つまり.txtファイルの編集方法を教えてください。 – eyllanesc

+0

最後の要素を印刷する場合は、代わりにtailを使う必要があります。少ない'tail -5 test.txt' – eyllanesc