import random import sys from PyQt5 import QtWidgets, QtGui, QtCore COLORS = [ # 17undertoneshttps://lospec.com/palette-list/17undertones '#000000', '#141923', '#414168', '#3A7FA7', '#35E3E3', '#8FD970', '#5EBB49', '#458352', '#DCD37B', '#FFFEE5', '#FFD035', '#CC9245', '#A15C3E', '#A42F3B', '#F45B7A', '#C24998', '#81588D', '#BCB0C2', '#FFFFFF', ] SPRAY_PARTICLES = 100 SPRAY_DIAMMETER = 10 class Canvas(QtWidgets.QLabel): def __init__(self): super().__init__() pixmap = QtGui.QPixmap(600, 300) self.setPixmap(pixmap) self.last_x, self.last_y = None, None self.pen_color = QtGui.QColor('#000000') def set_pen_color(self, c): self.pen_color = QtGui.QColor(c) def mouseMoveEvent(self, e): if self.last_x is None: self.last_x = e.x() self.last_y = e.y() return painter = QtGui.QPainter(self.pixmap()) p = painter.pen() p.setWidth(1) p.setColor(self.pen_color) painter.setPen(p) for n in range(SPRAY_PARTICLES): xo = random.gauss(0, SPRAY_DIAMMETER) yo = random.gauss(0, SPRAY_DIAMMETER) painter.drawPoint(e.x() + xo, e.y() + yo) self.update() self.last_x = e.x() self.last_y = e.y() def mouseReleaseEvent(self, e): self.last_x, self.last_y = None, None class QPaletteButton(QtWidgets.QPushButton): def __init__(self, color): super().__init__() self.setFixedSize(QtCore.QSize(24, 24)) self.color = color self.setStyleSheet("background-color: %s;" % color) class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.canvas = Canvas() w = QtWidgets.QWidget() l = QtWidgets.QVBoxLayout() w.setLayout(l) l.addWidget(self.canvas) palette = QtWidgets.QHBoxLayout() self.add_palette_buttons(palette) l.addLayout(palette) self.setCentralWidget(w) def add_palette_buttons(self, layout): for c in COLORS: b = QPaletteButton(c) b.pressed.connect(lambda color=c: self.canvas.set_pen_color(color)) layout.addWidget(b) if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) window = MainWindow() window.show() app.exec_()