PyQt5 Random Color GUI Application

  • Share this:

Code introduction


This code defines a simple GUI application based on PyQt5 that displays a window with a random color. It uses the PyQt5 modules QWidget, QColor, and Qt.


Technology Stack : PyQt5, QWidget, QColor, Qt

Code Type : Custom GUI application

Code Difficulty : Intermediate


                
                    
def random_color_widget():
    from PyQt5.QtWidgets import QWidget
    from PyQt5.QtGui import QColor
    from PyQt5.QtCore import Qt

    class ColorWidget(QWidget):
        def __init__(self):
            super().__init__()
            self.initUI()

        def initUI(self):
            self.setGeometry(300, 300, 250, 150)
            self.setWindowTitle('Random Color Widget')
            self.showRandomColor()

        def showRandomColor(self):
            random_color = QColor(
                Qt.red() + Qt.green() + Qt.blue()
            )
            self.setStyleSheet(f"background-color: {random_color.name()}")