"); //-->
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_())
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。