pyqt 中的 `less +F logfile`
`less +F logfile` in pyqt
我正在尝试弄清楚如何使用登录信息不断更新文本区域。我有以下内容(我的代码片段)
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
命令,我只是想查看文件的连续尾端。
我试过像这样使用线程,但无济于事
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
还有评论说你要改command,那我建议你把你的run_command函数改成下面这样:
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)
我正在尝试弄清楚如何使用登录信息不断更新文本区域。我有以下内容(我的代码片段)
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
命令,我只是想查看文件的连续尾端。
我试过像这样使用线程,但无济于事
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
还有评论说你要改command,那我建议你把你的run_command函数改成下面这样:
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)