I've searched for a while now and can't seem to find a good answer to the question of how best to handle font colors in PyQt5. What I've found are large stylesheets with many, many options. It seems unwieldy, but maybe that's just QT.
Say I have some code like this:
for k, v in do_something.items():
self.textBrowser.setStyleSheet("fontName='Times-Italic'")
self.textBrowser.setStyleSheet("color: rgb(244,0,0)")
self.textBrowser.append(str(k) + ': ' + str(v))
How can I get k and v to be different colors, without a newline being tossed into the mix. I'd like to have a red "k", a colon, then a white "v". In the terminal this was fairly trivial; I'd add some values for colors, then call them inline as so:
color_red2_on = "\033[01;31m"
color_red2_off = "\33[00m"
for k, v in do_something.items():
print(color_red2_on + '{:45}'.format(k) + color_red2_off + ':' + '{0}'.format(v))
Also, on a broader note, I've not really found a lot of guidance on text positioning with QT, like curses or what have you. Most of the tutorials I've found seem to focus on only how to create the various widgets and connect them, not how to work on their appearance or behaviors.
QTextBrowser being an inherent class of QTextEdit supports Html with css2.1, so you can use it as shown in the following example.
import sys
from PyQt5 import QtCore, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
QtWidgets.QWidget.__init__(self, *args, **kwargs)
lay = QtWidgets.QVBoxLayout(self)
self.textBrowser = QtWidgets.QTextBrowser()
lay.addWidget(self.textBrowser)
do_something = {"A": "A", "B": "B"}
cursor = self.textBrowser.textCursor()
for k, v in do_something.items():
cursor.insertHtml('''<p><span style="color: red; font-family:Times; font-style: italic;">{}</span>'''.format(k))
cursor.insertHtml(''':''')
cursor.insertHtml('''<span style="color: blue; font-family:Times; font-style: italic;">{}</span></p>'''.format(v))
cursor.insertHtml("<br>")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
To do it in a simple way you can attach a stylesheet.
import sys
from PyQt5 import QtCore, QtWidgets
css = '''
.left{
color: red;
font-family:Times;
font-style: italic;
}
.right{
color: blue;
font-family:Times;
font-style: italic;
}
'''
class Widget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
QtWidgets.QWidget.__init__(self, *args, **kwargs)
lay = QtWidgets.QVBoxLayout(self)
self.textBrowser = QtWidgets.QTextBrowser()
lay.addWidget(self.textBrowser)
do_something = {"A": "A", "B": "B"}
cursor = self.textBrowser.textCursor()
doc = self.textBrowser.document()
doc.setDefaultStyleSheet(css)
for k, v in do_something.items():
cursor.insertHtml('''<p><span class='left'>{}</span>'''.format(k))
cursor.insertHtml(''':''')
cursor.insertHtml('''<span class='right'>{}</span></p>'''.format(v))
cursor.insertHtml("<br>")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())