📜  Python – 如何在 pyqt5 中保存画布?

📅  最后修改于: 2022-05-13 01:54:39.437000             🧑  作者: Mango

Python – 如何在 pyqt5 中保存画布?

Python提供了很多选项来开发 GUI 应用程序,PyQt5 就是其中之一。 PyQt5 是跨平台的 GUI 工具包,一组用于 Qt v5 的Python绑定。由于该库提供的工具和简单性,人们可以非常轻松地开发交互式桌面应用程序。

在本教程中,我们将了解如何在 pyqt5 中保存画布。我们使用QPainter 类在画布上绘制东西。

QPainter: Qt 的绘画系统能够使用抗锯齿以提高渲染质量,以亚像素精度渲染矢量图形、图像和基于轮廓字体的文本。

在本文中,我们将创建一个画布并在其上绘制一条线,并了解如何将该画布作为图像文件保存在系统上的所需位置。下面是实现。

Python3
# importing modules
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import * import sys
 
# creating class for window
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
 
 
        title = "Paint and save Application"
 
        top = 400
        left = 400
        width = 800
        height = 600
 
        # setting title of window
        self.setWindowTitle(title)
 
        # setting geometry
        self.setGeometry(top, left, width, height)
 
        # creating canvas
        self.image = QImage(self.size(), QImage.Format_RGB32)
 
        # setting canvas color to white
        self.image.fill(Qt.white)
 
        # creating menu bar
        mainMenu = self.menuBar()
 
        # adding file menu in it
        fileMenu = mainMenu.addMenu("File")
 
        # creating save action
        saveAction = QAction("Save", self)
 
        # setting save action shortcut
        saveAction.setShortcut("Ctrl + S")
 
        # adding save action to filemenu
        fileMenu.addAction(saveAction)
 
        # setting triggered method
        saveAction.triggered.connect(self.save)
 
        # calling draw_something method
        self.draw_something()
 
    # paintEvent for creating blank canvas
    def paintEvent(self, event):
        canvasPainter = QPainter(self)
        canvasPainter.drawImage(self.rect(), self.image,
                                      self.image.rect())
         
    # this method will draw a line
    def draw_something(self):
         
        painter = QPainter(self.image)
         
        painter.setPen(QPen(Qt.black, 5, Qt.SolidLine,
                            Qt.RoundCap, Qt.RoundJoin))
         
        # drawing a line
        painter.drawLine(100, 100, 300, 300)
         
        # updating it to canvas
        self.update()
     
    # save method
    def save(self):
         
        # selecting file path
        filePath, _ = QFileDialog.getSaveFileName(self, "Save Image", "",
                         "PNG(*.png);;JPEG(*.jpg *.jpeg);;All Files(*.*) ")
 
        # if file path is blank return back
        if filePath == "":
            return
         
        # saving canvas at desired path
        self.image.save(filePath)
 
# main method
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
     
    # looping for window
    sys.exit(app.exec())


输出 :
运行代码窗口后,将出现一个白色画布,在该画布上绘制线条。

点击文件菜单后会出现保存按钮。

当按下保存按钮时,它会询问位置和名称,图像将保存在所需的位置。

此代码将以 png 格式将画布保存在所需位置。