专栏中心

EEPW首页 > 专栏 > 使用 Python 和 PyQt5 制作简单的绘图软件

使用 Python 和 PyQt5 制作简单的绘图软件

发布人:小橘子lala 时间:2025-01-02 来源:工程师 发布文章

import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QGraphicsView, QGraphicsScene from PyQt5.QtGui import QPainter, QPen, QColor from PyQt5.QtCore import Qt # 绘图窗口类 class DrawingWidget(QWidget):    def __init__(self):        super().__init__()        self.init_ui()    def init_ui(self):        self.layout = QVBoxLayout()        self.scene = QGraphicsScene(self)        self.view = QGraphicsView(self.scene)        self.layout.addWidget(self.view)        self.clear_button = QPushButton('清空画布')        self.clear_button.clicked.connect(self.clear_canvas)        self.layout.addWidget(self.clear_button)        self.setWindowTitle('简易绘图软件')        self.setLayout(self.layout)    def paintEvent(self, event):        painter = QPainter(self)        painter.setPen(QPen(QColor('black'), 2, Qt.SolidLine))        for item in self.scene.items():            painter.drawPath(item.path())    def clear_canvas(self):        self.scene.clear() if __name__ == '__main__':    app = QApplication(sys.argv)    window = DrawingWidget()    window.show()    sys.exit(app.exec_())

专栏文章内容及配图由作者撰写发布,仅供工程师学习之用,如有侵权或者其他违规问题,请联系本站处理。 联系我们

关键词:
更多 培训课堂
更多 焦点
更多 视频

技术专区