PyQt5 Color Selection Dialog Example

  • Share this:

Code introduction


This function creates a simple PyQt5 application that includes a popup color selection dialog, allowing users to choose a color.


Technology Stack : PyQt5, QWidget, QColorDialog, QColor

Code Type : Custom function

Code Difficulty : Intermediate


                
                    
def random_color_widget(parent=None):
    from PyQt5.QtWidgets import QWidget, QColorDialog
    from PyQt5.QtGui import QColor

    class ColorWidget(QWidget):
        def __init__(self, parent=None):
            super(ColorWidget, self).__init__(parent)
            self.initUI()

        def initUI(self):
            self.color_dialog = QColorDialog(self)
            self.color_dialog.setOptions(QColorDialog.ShowAlphaChannel)
            self.color_dialog.setCurrentColor(QColor(255, 0, 0))
            self.color_dialog.open()

    return ColorWidget