python pyqt5 different item color in same qlist

Solutions on MaxInterview for python pyqt5 different item color in same qlist by the best coders in the world

showing results for - "python pyqt5 different item color in same qlist"
Serena
29 Sep 2017
1from PyQt5.QtGui import *
2from PyQt5.QtWidgets import *
3
4colors = [ '#7fc97f', '#beaed4', '#fdc086', '#ffff99', '#386cb0', '#f0027f', '#bf5b17', '#666666']
5
6class MainWindow(QMainWindow):
7
8    def __init__(self, *args, **kwargs):
9        super(MainWindow, self).__init__(*args, **kwargs)
10
11        w = QListWidget()
12        for n in range(8):
13            i = QListWidgetItem('%s' % n)
14            i.setBackground( QColor(colors[n]) )
15            w.addItem(i)
16
17        self.setCentralWidget(w)
18
19        self.show()
20
21
22app = QApplication([])
23w = MainWindow()
24app.exec_()