首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pyqt中的` `less +F logfile`

pyqt中的` `less +F logfile`
EN

Stack Overflow用户
提问于 2017-08-05 00:06:18
回答 1查看 54关注 0票数 0

我正在尝试弄清楚如何使用loginfo持续更新文本区。我有以下内容(摘自我的代码片段)

代码语言:javascript
复制
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_())

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

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

代码语言:javascript
复制
runThread = threading.Thread(target=self.run_command("less +F test.txt"))
runThread.daemon = True
runThread.start()

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-05 01:30:04

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

代码语言:javascript
复制
timer = core.QTimer(self)
timer.timeout.connect(lambda: self.run_command("less +F test.txt"))
timer.start(10) # milliseconds

我建议你修改你的run_command函数,如下所示:

代码语言:javascript
复制
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:

代码语言:javascript
复制
self.cmd = "less +F otherFile.txt" 

完整示例:

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45511236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档