PyQt5–创建绘画应用

PyQt5–创建绘画应用

Python为开发GUI应用程序提供了很多选择,PyQt5就是其中之一。PyQt5是跨平台的GUI工具包,是Qt v5的一套python绑定。 由于这个库所提供的工具和简单性,人们可以非常容易地开发一个交互式的桌面应用程序。

在这篇文章中,我们将看到如何使用PyQt5创建一个画图程序。我们的绘画程序将包括以下内容

功能。

  1. 用户可以选择不同的画笔尺寸
  2. 用户可以选择不同的画笔颜色
  3. 保存画布
  4. 一次性清除整个画布

PyQt5--创建绘画应用

设计小部件的步骤 –

1.创建一个窗口,设置其几何形状和标题

2.创建菜单栏

3.在菜单栏内添加不同的菜单,即文件菜单、尺寸菜单和颜色菜单

4.在文件菜单中添加保存和清除动作

PyQt5--创建绘画应用

5.在画笔尺寸菜单中添加不同的画笔尺寸动作

PyQt5--创建绘画应用

6.在画笔颜色菜单中添加不同的画笔颜色动作

PyQt5--创建绘画应用

7.创建一个白色画布并将其添加到窗口中。

后端步骤:

1.创建不同的变量:绘图标志检查当前是否在绘图并将其设置为假,画笔大小变量设置当前画笔大小,画笔颜色设置当前画笔颜色,当前位置变量知道光标的位置

2.为清除和保存部件添加动作

3.在清除动作中用白色填充画布

4.在保存动作中保存画布

5.为各种画笔大小和颜色添加动作(方法),以设置大小和颜色

6.创建绘画事件,在屏幕上绘制白色画布

7.创建方法来知道当鼠标左键被按下时,在该方法中使绘图标志为真并改变当前位置

8.创建鼠标移动的方法,在此方法中检查绘图标志是否为真,按钮是否仍被按下,然后使用绘图器对象绘图并改变位置。

9.创建一个方法来知道鼠标按钮是否被释放,在这个方法中使绘图标志为假。

下面是实现的过程

# importing libraries
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
 
# window class
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
 
        # setting title
        self.setWindowTitle("Paint with PyQt5")
 
        # setting geometry to main window
        self.setGeometry(100, 100, 800, 600)
 
        # creating image object
        self.image = QImage(self.size(), QImage.Format_RGB32)
 
        # making image color to white
        self.image.fill(Qt.white)
 
        # variables
        # drawing flag
        self.drawing = False
        # default brush size
        self.brushSize = 2
        # default color
        self.brushColor = Qt.black
 
        # QPoint object to tract the point
        self.lastPoint = QPoint()
 
        # creating menu bar
        mainMenu = self.menuBar()
 
        # creating file menu for save and clear action
        fileMenu = mainMenu.addMenu("File")
 
        # adding brush size to main menu
        b_size = mainMenu.addMenu("Brush Size")
 
        # adding brush color to ain menu
        b_color = mainMenu.addMenu("Brush Color")
 
        # creating save action
        saveAction = QAction("Save", self)
        # adding short cut for save action
        saveAction.setShortcut("Ctrl + S")
        # adding save to the file menu
        fileMenu.addAction(saveAction)
        # adding action to the save
        saveAction.triggered.connect(self.save)
 
        # creating clear action
        clearAction = QAction("Clear", self)
        # adding short cut to the clear action
        clearAction.setShortcut("Ctrl + C")
        # adding clear to the file menu
        fileMenu.addAction(clearAction)
        # adding action to the clear
        clearAction.triggered.connect(self.clear)
 
        # creating options for brush sizes
        # creating action for selecting pixel of 4px
        pix_4 = QAction("4px", self)
        # adding this action to the brush size
        b_size.addAction(pix_4)
        # adding method to this
        pix_4.triggered.connect(self.Pixel_4)
 
        # similarly repeating above steps for different sizes
        pix_7 = QAction("7px", self)
        b_size.addAction(pix_7)
        pix_7.triggered.connect(self.Pixel_7)
 
        pix_9 = QAction("9px", self)
        b_size.addAction(pix_9)
        pix_9.triggered.connect(self.Pixel_9)
 
        pix_12 = QAction("12px", self)
        b_size.addAction(pix_12)
        pix_12.triggered.connect(self.Pixel_12)
 
        # creating options for brush color
        # creating action for black color
        black = QAction("Black", self)
        # adding this action to the brush colors
        b_color.addAction(black)
        # adding methods to the black
        black.triggered.connect(self.blackColor)
 
        # similarly repeating above steps for different color
        white = QAction("White", self)
        b_color.addAction(white)
        white.triggered.connect(self.whiteColor)
 
        green = QAction("Green", self)
        b_color.addAction(green)
        green.triggered.connect(self.greenColor)
 
        yellow = QAction("Yellow", self)
        b_color.addAction(yellow)
        yellow.triggered.connect(self.yellowColor)
 
        red = QAction("Red", self)
        b_color.addAction(red)
        red.triggered.connect(self.redColor)
 
 
    # method for checking mouse cicks
    def mousePressEvent(self, event):
 
        # if left mouse button is pressed
        if event.button() == Qt.LeftButton:
            # make drawing flag true
            self.drawing = True
            # make last point to the point of cursor
            self.lastPoint = event.pos()
 
    # method for tracking mouse activity
    def mouseMoveEvent(self, event):
         
        # checking if left button is pressed and drawing flag is true
        if (event.buttons() & Qt.LeftButton) & self.drawing:
             
            # creating painter object
            painter = QPainter(self.image)
             
            # set the pen of the painter
            painter.setPen(QPen(self.brushColor, self.brushSize,
                            Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
             
            # draw line from the last point of cursor to the current point
            # this will draw only one step
            painter.drawLine(self.lastPoint, event.pos())
             
            # change the last point
            self.lastPoint = event.pos()
            # update
            self.update()
 
    # method for mouse left button release
    def mouseReleaseEvent(self, event):
 
        if event.button() == Qt.LeftButton:
            # make drawing flag false
            self.drawing = False
 
    # paint event
    def paintEvent(self, event):
        # create a canvas
        canvasPainter = QPainter(self)
         
        # draw rectangle  on the canvas
        canvasPainter.drawImage(self.rect(), self.image, self.image.rect())
 
    # method for saving canvas
    def save(self):
        filePath, _ = QFileDialog.getSaveFileName(self, "Save Image", "",
                          "PNG(*.png);;JPEG(*.jpg *.jpeg);;All Files(*.*) ")
 
        if filePath == "":
            return
        self.image.save(filePath)
 
    # method for clearing every thing on canvas
    def clear(self):
        # make the whole canvas white
        self.image.fill(Qt.white)
        # update
        self.update()
 
    # methods for changing pixel sizes
    def Pixel_4(self):
        self.brushSize = 4
 
    def Pixel_7(self):
        self.brushSize = 7
 
    def Pixel_9(self):
        self.brushSize = 9
 
    def Pixel_12(self):
        self.brushSize = 12
 
    # methods for changing brush color
    def blackColor(self):
        self.brushColor = Qt.black
 
    def whiteColor(self):
        self.brushColor = Qt.white
 
    def greenColor(self):
        self.brushColor = Qt.green
 
    def yellowColor(self):
        self.brushColor = Qt.yellow
 
    def redColor(self):
        self.brushColor = Qt.red
 
 
 
# create pyqt5 app
App = QApplication(sys.argv)
 
# create the instance of our Window
window = Window()
 
# showing the window
window.show()
 
# start the app
sys.exit(App.exec())

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程