Random Background Color Button

  • Share this:

Code introduction


Create a button with a random background color. When the user clicks the button, the background color will change.


Technology Stack : PyQt5, QPushButton, QApplication, QColor

Code Type : PyQt5 GUI Application

Code Difficulty : Intermediate


                
                    
def random_color_button():
    from PyQt5.QtWidgets import QPushButton, QApplication
    from PyQt5.QtGui import QColor

    app = QApplication([])
    button = QPushButton("Click Me!")
    button.setStyleSheet(f"background-color: {QColor().name()}")
    button.show()
    app.exec_()