You can download this code by clicking the button below.
This code is now available for download.
This code creates a PyQt-based window where the background color is randomly generated each time the program is run.
Technology Stack : PyQt, PyQt5.QtWidgets, PyQt5.QtCore, random
Code Type : Graphical user interface
Code Difficulty : Intermediate
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import Qt
import sys
import random
def random_color():
def generate_color():
return random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
def hex_color(r, g, b):
return f'#{r:02x}{g:02x}{b:02x}'
color = generate_color()
return hex_color(*color)
class ColorWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Random Color Window')
self.show()
self.setStyleSheet(f'background-color: {random_color()}')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = ColorWindow()
sys.exit(app.exec_())