In this PyQt5 tutorial, we will learn how to change QListWidget item font color using few lines of code in Python.
Buy Me a Coffee? Your support is much appreciated!
PayPal Me: https://www.paypal.me/jiejenn/5
Venmo: @Jie-Jenn
Source Code:
import sys
from PyQt5.QtWidgets import QApplication, QListWidget, QListWidgetItem
from PyQt5.QtCore import Qt
class AppDemo(QListWidget):
def __init__(self):
super().__init__()
self.resize(1200, 800)
self.setStyleSheet('font-size: 50px')
colors = ['Blue', 'Yellow', 'Green', 'Purple']
toColors = [True, False, False, True]
for color, cond in zip(colors, toColors):
item = QListWidgetItem(color)
if cond:
item.setForeground(Qt.red)
self.addItem(item)
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = AppDemo()
demo.show()
sys.exit(app.exec_())