📜  PyQt5 QColorDialog – 子类型(1)

📅  最后修改于: 2023-12-03 14:45:48.023000             🧑  作者: Mango

PyQt5 QColorDialog – 子类型

The PyQt5 QColorDialog module provides a dialog widget for selecting a color. The QColorDialog widget allows the user to choose a color. It provides different options and subtypes, such as choosing a color on a color wheel, selecting a color from a predefined color set, etc. This module is a part of PyQt5 module, which is a set of Python bindings for the Qt toolkit.

Subtypes of QColorDialog widget:
QColorDialog.getColor()

This method presents a dialog for selecting a single color. This method can be called both with optional and required parameters. The optional parameters allow the user to customize the dialog and control the appearance of different subtypes.

QColorDialog.getRgba()

This method returns the chosen color in RGBA format.

QColorDialog.getColorF()

This method returns the chosen color in floating-point format.

Examples
Example 1: Using getColor() method
import sys
from PyQt5.QtWidgets import QApplication, QColorDialog
 
class ColorDialog(QWidget):
 
    def __init__(self):
        super().__init__()
        self.initUI()
         
    def initUI(self):
 
        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('PyQt5 QColorDialog')
         
        col = QColor(0, 0, 0)        
        self.btn = QPushButton('Choose Color', self)
        self.btn.move(20, 20)
        self.btn.clicked.connect(self.showColorDialog)
         
        self.frm = QFrame(self)
        self.frm.setGeometry(130, 22, 100, 100)
        self.frm.setStyleSheet('QWidget { background-color: %s }' % col.name())
         
        self.show()
         
    def showColorDialog(self):
 
        col = QColorDialog.getColor()
         
        if col.isValid():
            self.frm.setStyleSheet('QWidget { background-color: %s }' % col.name())
 
 
if __name__ == '__main__':
     
    app = QApplication(sys.argv)
    ex = ColorDialog()
    sys.exit(app.exec_())
Example 2: Using getRgba() and getColorF() methods:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel, QColorDialog
from PyQt5.QtGui import QColor
 
class ColorDialog(QWidget):
 
    def __init__(self):
        super().__init__()
        self.initUI()
         
    def initUI(self):
 
        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('PyQt5 QColorDialog')
         
        self.btn = QPushButton('Choose Color', self)
        self.btn.move(20, 20)
        self.btn.clicked.connect(self.showColorDialog)
         
        self.lbl = QLabel('Chosen Color', self)
        self.lbl.move(220, 20)
         
        vbox = QVBoxLayout()
        vbox.addWidget(self.btn)
        vbox.addStretch()
        vbox.addWidget(self.lbl)
         
        self.setLayout(vbox)
         
        self.show()
         
    def showColorDialog(self):
 
        col = QColorDialog.getColor()
         
        if col.isValid():
            self.lbl.setText('Chosen Color: ' + col.name())
            rgba = col.getRgb()
            self.lbl.setStyleSheet('QLabel { background-color : %s }' % col.name())
            print('Chosen Color (RGBA):', rgba)
            rgba_f = col.getRgbF()
            print('Chosen Color (RGBF):', rgba_f)
 
 
if __name__ == '__main__':
     
    app = QApplication(sys.argv)
    ex = ColorDialog()
    sys.exit(app.exec_())

In the first example, we have used the getColor() method to open a color dialog, where the user can choose and select a color. Once the user selects the color, the setColor() method updates the background color of the QFrame widget.

In the 2nd example, we have demonstrated the use of different methods getColorF() and getRgba(). These methods can be used to return the chosen color in different formats, such as RGBA and floating-point format.

These examples showcase how PyQt5 QColorDialog subtypes can be used to provide a versatile color picker feature in applications.