import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class QColorDialogDemo(QWidget):
def __init__(self):
super(QColorDialogDemo, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('颜色对话框')
self.colorButton = QPushButton('选择颜色')
self.colorButton.clicked.connect(self.getColor)
self.colorLable = QLabel('Hello, 测试颜色例子')
self.colorButton1 = QPushButton('设置背景色')
self.colorButton1.clicked.connect(self.getBGColor)
layout = QVBoxLayout()
layout.addWidget(self.colorButton)
layout.addWidget(self.colorLable)
layout.addWidget(self.colorButton1)
self.setLayout(layout)
def getColor(self):
color = QColorDialog.getColor()
p = QPalette()
if color:
p.setColor(QPalette.WindowText, color)
self.colorLable.setPalette(p)
def getBGColor(self):
color = QColorDialog.getColor()
p = QPalette()
if color:
p.setColor(QPalette.Window, color)
self.colorLable.setAutoFillBackground(True)
self.colorLable.setPalette(p)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = QColorDialogDemo()
win.show()
sys.exit(app.exec_())