马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请教大佬,小弟在用pyqt写界面,其中需要日志记录,我采用的是QextBrowser进行记录,日志中有关键字(‘INFO’、‘WARNING’、'ERROR'、‘DEBUG’),信息部分,还有时间部分,我需要的是各个关键字有颜色区分,同时日志中每一行关键字对齐,信息部分对齐,时间部分对齐,这样看起来比较整洁美观,但是一直实现不了对齐,如图,不知道怎么解决。
# -*- coding: utf-8 -*-
import sys
import os
import time
from PyQt4 import QtGui,QtCore
##from PyQt4.QtGui import *
##from PyQt4.QtCore import *
class Show_LOGfile(QtGui.QDialog):
def __init__(self, parent=None):
super(Show_LOGfile, self).__init__(parent)
self.LOG_browser = QtGui.QTextBrowser()
self.load_button=QtGui.QPushButton("open")
self.clear_button=QtGui.QPushButton("delete")
self.label=QtGui.QLabel("")
buttonLayout=QtGui.QHBoxLayout()
buttonLayout.addWidget(self.label)
buttonLayout.addStretch()
buttonLayout.addWidget(self.load_button)
buttonLayout.addWidget(self.clear_button)
layout = QtGui.QGridLayout()
layout.addLayout(buttonLayout,0, 0)
layout.addWidget(self.LOG_browser,1, 0)
self.setLayout(layout)
self.resize(840, 700)
self.connect(self.load_button,QtCore.SIGNAL("clicked()"), self.load_file)
self.connect(self.clear_button,QtCore.SIGNAL("clicked()"), self.clear_file)
def load_file(self):
m1='WARNING:The speed is too high'
m2='ERROR:The tyre pressure is low,pleaese stop the car'
m3='INFO:The Main power is enabled'
m4='DEBUG:GPS MAP is missed'
log_message=[m1,m2,m3,m4]
for log in log_message:
currentDateTime = QtCore.QDateTime.currentDateTime()
currentDateStr = currentDateTime.toString("[yyyy.MM.dd hh:mm:ss ddd] ")
s1=log.split(':')[0]
s2=log.split(':')[1]
if s1=='INFO':
self.LOG_browser.append("<font color=black size=5 >%s</font><font size=5>%s</font><font color=blue size=5 >%s</font>" % ('INFO:',s2,currentDateStr))
elif s1=='WARNING':
self.LOG_browser.append("<font color=yellow size=5 >%s</font><font size=5>%s</font><font color=blue size=5 >%s</font>" % ('WARNING:',s2,currentDateStr))
elif s1=='ERROR':
self.LOG_browser.append("<font color=red size=5 >%s</font><font size=5>%s</font><font color=blue size=5 >%s</font>" % ('ERROR:',s2,currentDateStr))
else:
self.LOG_browser.append("<font color=brown size=5 >%s</font><font size=5>%s</font><font color=blue size=5 >%s</font>" % ('DEBUG:',s2,currentDateStr))
self.LOG_browser.append("<font color=red size=5 >%s</font><font size=5>%s</font><font color=blue size=5 >%s</font>" % ('','',''))
self.LOG_browser.moveCursor(QtGui.QTextCursor.End)
def clear_file(self):
self.LOG_browser.clear()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = Show_LOGfile()
window.show()
sys.exit(app.exec_())
|