我正在尝试弄清楚如何使用loginfo持续更新文本区。我有以下内容(摘自我的代码片段)
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命令,这样我就不会阻塞主应用程序,但我不确定如何最好地做到这一点。
发布于 2017-08-05 01:30:04
使用线程是一种选择,但在你的情况下不是最好的,我建议使用计时器,因为它对GUI更用户友好。
timer = core.QTimer(self)
timer.timeout.connect(lambda: self.run_command("less +F test.txt"))
timer.start(10) # milliseconds我建议你修改你的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)https://stackoverflow.com/questions/45511236
复制相似问题