Develop
PyQt에서 QPlainText를 이용해 정보 출력하기(색상 변경)
Dork94
2019. 4. 14. 03:21
프로그램에서 특정 정보를 출력할 목적으로 Widget를 찾아보았는데, QPlainText을 추천해서 간단하게 구현해봤다.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget
from PyQt5.QtWidgets import QPlainTextEdit
alertHtml = "<font color=\"DeepPink\">";
notifyHtml = "<font color=\"Lime\">";
infoHtml = "<font color=\"Aqua\">";
endHtml = "</font><br>";
__author__='d0rk'
class my_app(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('PyQt QPlaintext Example')
self.log_window()
# self.resize(1280, 800)
self.center()
self.show()
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def log_window(self):
widget = QPlainTextEdit(self)
widget.setReadOnly(True)
widget.appendPlainText('Simple Test')
widget.appendHtml(alertHtml + 'Alert Test')
widget.appendHtml(notifyHtml + 'Notify Test')
widget.appendHtml(infoHtml + 'Info Test')
widget.appendHtml(endHtml + 'Endhtml')
widget.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = my_app()
sys.exit(app.exec_())
appendPlainText 함수를 이용해, 일반 text를 출력할 수 있고, appendHtml을 이용해 html 태그를 이용할 수 있는데, 이를 이용해 color의 색상을 변경할 수 있다.