PyQT5 颜色对话框

PyQt5 支持一种名为 QColorDialog 的颜色选择器。此对话框是你在绘画或图形程序中看到的典型对话框。

要从 PyQt5 对话框中获取颜色,只需调用:

color = QColorDialog.getColor()

PyQt5 颜色对话框

PyQt5 颜色对话框示例

下面的示例在单击按钮后打开 QColorDialog,并返回所选颜色。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QColorDialog
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtGui import QColor

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 color dialog - tastones.com'
        self.left = 10
        self.top = 10
        self.width = 320
        self.height = 200
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        button = QPushButton('Open color dialog', self)
        button.setToolTip('Opens color dialog')
        button.move(10,10)
        button.clicked.connect(self.on_click)

        self.show()

    @pyqtSlot()
    def on_click(self):
        openColorDialog(self)


def openColorDialog(self):
    color = QColorDialog.getColor()

    if color.isValid():
        print(color.name())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())