PyQt5 QColorDialog – 获取自定义颜色

  • Post category:Python

下面是针对“PyQt5 QColorDialog-获取自定义颜色”的使用攻略:

1. PyQt5 QColorDialog简介

QColorDialogPyQt5中的一个颜色对话框,能够让用户选择颜色。通过该对话框可以获取预定义颜色、系统或者平台定义的颜色,或者是用户在对话框中自定义的颜色。

2. QColorDialog语法

以下是QColorDialog类的用法:

color = QColorDialog.getColor(initial: Union[QColor, int], parent: QWidget, title: str = '', options: Union[QColorDialog.ColorDialogOption, QColorDialog.ColorDialogOptions] = QColorDialog.ColorDialogOptions() )

2.1 参数说明

  • initial:初始颜色。
  • parent:对话框的父控件。
  • title:对话框的标题。
  • options:对话框的选项。

2.2 返回值说明

  • color:所选颜色。

注:如果用户取消选择,那么返回的color值为None

3. 示例说明

下面是两个使用QColorDialog的示例说明。

3.1 示例1: 获取选中的颜色,并在QTextEdit中显示

以下是获取用户自定义的颜色,并在QTextEdit中显示选中的颜色:

from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QTextEdit, QColorDialog
from PyQt5.QtGui import QColor
import sys


class Example(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.textEdit = QTextEdit()
        self.setCentralWidget(self.textEdit)
        self.statusBar()

        # 创建一个QT Action,用于触发颜色对话框的显示
        newColorAction = QAction('Select color', self)
        newColorAction.setShortcut('Ctrl+Shift+C')
        newColorAction.setStatusTip('Select color')
        newColorAction.triggered.connect(self.showColorDialog)
        self.toolbar = self.addToolBar('ColorDialog')
        self.toolbar.addAction(newColorAction)

        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('QColorDialog')
        self.show()

    def showColorDialog(self):
        # 获取所选颜色
        color = QColorDialog.getColor()
        if color.isValid():
            self.textEdit.setTextColor(color)

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

代码解释:

  • initUI()函数中创建了一个QTextEdit控件,以及一个工具条。工具条上使用QAction触发弹出颜色对话框。
  • showColorDialog()函数用于显示颜色对话框并获取选中的颜色。此样例只获取所选颜色,并用于设置文本颜色。

3.2 示例2: 获取选中颜色并开始绘图

下面的示例展示了获取用户自定义的颜色并用于绘图。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QColorDialog
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.color = QColor(0, 0, 0)
        self.showDialog()
        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('QColorDialog')
        self.show()

    def showDialog(self):
        # 获取选定的颜色
        color = QColorDialog.getColor()
        if color.isValid():
            self.color = color

    def paintEvent(self, event):
        qp = QPainter()
        qp.begin(self)
        self.drawLines(qp)
        qp.end()

    def drawLines(self, qp):
        pen = QPen(self.color, 8, Qt.SolidLine)
        qp.setPen(pen)
        qp.drawLine(20, 40, 250, 40)


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

代码解析:

  • Example类继承QWidget类并实现了paintEvent()函数和showDialog()函数。
  • showDialog()函数用于获取所选颜色。
  • paintEvent()函数是绘制窗口内容的函数,使用drawLines()函数绘制一条直线,线条的颜色使用选定的color对象。

以上就是PyQt5 QColorDialog获取自定义颜色的使用攻略,通过标准的markdown文本格式,展示了如何使用该库,并且提供了两个实例,如有不足请指正。