Advertisement
shh_algo_PY

Paint Project

Dec 17th, 2022
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. import sys
  2. from PyQt5 import QtCore, QtGui, QtWidgets, uic
  3. from PyQt5.QtCore import Qt
  4.  
  5. class Canvas(QtWidgets.QLabel):
  6.  
  7.     def __init__(self):
  8.         super().__init__()
  9.         pixmap = QtGui.QPixmap(1600, 1300)
  10.         self.setPixmap(pixmap)
  11.  
  12.         self.last_x, self.last_y = None, None
  13.         self.pen_color = QtGui.QColor('#000000')
  14.  
  15.     def set_pen_color(self, c):
  16.         self.pen_color = QtGui.QColor(c)
  17.  
  18.     def mouseMoveEvent(self, e):
  19.         if self.last_x is None: # First event.
  20.             self.last_x = e.x()
  21.             self.last_y = e.y()
  22.             return # Ignore the first time.
  23.  
  24.         painter = QtGui.QPainter(self.pixmap())
  25.         p = painter.pen()
  26.         p.setWidth(10)
  27.         p.setColor(self.pen_color)
  28.         painter.setPen(p)
  29.         painter.drawLine(self.last_x, self.last_y, e.x(), e.y())
  30.         painter.end()
  31.         self.update()
  32.  
  33.         # Update the origin for next time.
  34.         self.last_x = e.x()
  35.         self.last_y = e.y()
  36.  
  37.     def mouseReleaseEvent(self, e):
  38.         self.last_x = None
  39.         self.last_y = None
  40.  
  41. COLORS = [
  42. # 17 undertones https://lospec.com/palette-list/17undertones
  43. '#000000', '#141923', '#414168', '#3a7fa7', '#35e3e3', '#8fd970', '#5ebb49',
  44. '#458352', '#dcd37b', '#fffee5', '#ffd035', '#cc9245', '#a15c3e', '#a42f3b',
  45. '#f45b7a', '#c24998', '#81588d', '#bcb0c2', '#ffffff',
  46. ]
  47.  
  48.  
  49. class QPaletteButton(QtWidgets.QPushButton):
  50.  
  51.     def __init__(self, color):
  52.         super().__init__()
  53.         self.setFixedSize(QtCore.QSize(50,50))
  54.         self.color = color
  55.         self.setStyleSheet("background-color: %s;" % color)
  56.  
  57. class MainWindow(QtWidgets.QMainWindow):
  58.  
  59.     def __init__(self):
  60.         super().__init__()
  61.  
  62.         self.canvas = Canvas()
  63.  
  64.         w = QtWidgets.QWidget()
  65.         l = QtWidgets.QVBoxLayout()
  66.         w.setLayout(l)
  67.         l.addWidget(self.canvas)
  68.  
  69.         palette = QtWidgets.QHBoxLayout()
  70.         self.add_palette_buttons(palette)
  71.         l.addLayout(palette)
  72.  
  73.         self.setCentralWidget(w)
  74.  
  75.     def add_palette_buttons(self, layout):
  76.         for c in COLORS:
  77.             b = QPaletteButton(c)
  78.             b.pressed.connect(lambda c=c: self.canvas.set_pen_color(c))
  79.             layout.addWidget(b)
  80.  
  81.  
  82. app = QtWidgets.QApplication(sys.argv)
  83. window = MainWindow()
  84. window.show()
  85. app.exec_()        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement