最近用Qt做GUI,想看看QColor中预定义的颜色都有那些,于是编了一个小程序:
from PyQt4.QtGui import QColor, QWidget
from PyQt4 import QtGui
class mywindow(QWidget):
def __init__(self):
super(mywindow, self).__init__()
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = mywindow()
grid = QtGui.QGridLayout()
i = j = 0
row = 15
print(len(QColor.colorNames()))
for name in QColor.colorNames():
label = QtGui.QLabel()
label.setText(name)
#通过亮度决定文字颜色
if QColor(name).getHsv()[2] > 200:
label.setStyleSheet(
"QLabel{background-color: " + name + ";font: 16px;color: black;}")
else:
label.setStyleSheet(
"QLabel{background-color: " + name + ";font: 16px;color: white;}")
grid.addWidget(label, i % row, j)
temp = i % row
i += 1
if i % row
j += 1
w.setLayout(grid)
w.show()
sys.exit(app.exec_())
结果如下图: