Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python `减去+;pyqt中的F logfile`_Python_Pyqt_Pyqt4 - Fatal编程技术网

Python `减去+;pyqt中的F logfile`

Python `减去+;pyqt中的F logfile`,python,pyqt,pyqt4,Python,Pyqt,Pyqt4,我想知道如何用loginfo不断更新文本区域。我有以下内容(代码片段) 我这里的问题是程序挂起。我希望能够连续显示输出,并最终能够终止该命令并运行less+F otherFile.txt。我并不专门使用less命令,我只想看到文件的连续结尾 我尝试过使用线程,就像这样,但没有用 runThread = threading.Thread(target=self.run_command("less +F test.txt")) runThread.daemon = True runThread.st

我想知道如何用loginfo不断更新文本区域。我有以下内容(代码片段)

我这里的问题是程序挂起。我希望能够连续显示输出,并最终能够终止该命令并运行
less+F otherFile.txt
。我并不专门使用
less
命令,我只想看到文件的连续结尾

我尝试过使用线程,就像这样,但没有用

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

我得到的印象是,我需要在不同的线程上运行ostream命令,这样我就不会阻塞主应用程序,但我不确定如何最好地做到这一点。

使用线程是一种选择,但不是最好的选择,我建议使用计时器,因为它对GUI更友好

timer = core.QTimer(self)
timer.timeout.connect(lambda: self.run_command("less +F test.txt"))
timer.start(10) # milliseconds
另外,如果您希望更改命令,我建议您将run_命令功能修改为以下内容:

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)
然后,要更改命令,只需将新字符串传递到self.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)

这几乎是完美的。我在实际用例中遇到了两个问题。第一个是,当文本在
test.txt
中生成时,我们的窗口没有更新(就像
less+F
一样)。另一个是我希望我的输出只显示尾部。这总是从顶部开始并显示整个文件。第一点,对我来说,它工作得很好,你可以向我解释你是如何进行测试的,即你是如何编辑.txt文件的。第二点,如果你想打印最后的元素,你必须使用tail而不是less
tail-5test.txt
我真的很笨,
tail
在这个场景中更有意义(因为计时器)。我现在可以用了。谢谢
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)