用python写一个有pyqt界面的画图程序
时间: 2023-05-20 13:04:52 浏览: 177
pyQt界面编程
好的,我可以为您提供一个基于Python和PyQt的画图程序。以下是代码示例:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QFileDialog, QGraphicsScene, QGraphicsView
from PyQt5.QtGui import QPixmap, QPainter, QPen, QColor
from PyQt5.QtCore import Qt, QPoint, QRectF
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("画图程序")
self.setGeometry(100, 100, 800, 600)
self.scene = QGraphicsScene(self)
self.view = QGraphicsView(self.scene, self)
self.view.setGeometry(0, 0, 800, 600)
self.pen = QPen(QColor(0, 0, 0), 2)
self.drawing = False
self.lastPoint = QPoint()
self.create_actions()
self.create_menus()
def create_actions(self):
self.new_action = QAction("新建", self)
self.new_action.setShortcut("Ctrl+N")
self.new_action.triggered.connect(self.new)
self.open_action = QAction("打开", self)
self.open_action.setShortcut("Ctrl+O")
self.open_action.triggered.connect(self.open)
self.save_action = QAction("保存", self)
self.save_action.setShortcut("Ctrl+S")
self.save_action.triggered.connect(self.save)
self.exit_action = QAction("退出", self)
self.exit_action.setShortcut("Ctrl+Q")
self.exit_action.triggered.connect(self.close)
self.pen_action = QAction("画笔", self)
self.pen_action.setShortcut("Ctrl+P")
self.pen_action.triggered.connect(self.set_pen)
self.eraser_action = QAction("橡皮擦", self)
self.eraser_action.setShortcut("Ctrl+E")
self.eraser_action.triggered.connect(self.set_eraser)
def create_menus(self):
self.file_menu = self.menuBar().addMenu("文件")
self.file_menu.addAction(self.new_action)
self.file_menu.addAction(self.open_action)
self.file_menu.addAction(self.save_action)
self.file_menu.addAction(self.exit_action)
self.tool_menu = self.menuBar().addMenu("工具")
self.tool_menu.addAction(self.pen_action)
self.tool_menu.addAction(self.eraser_action)
def new(self):
self.scene.clear()
def open(self):
file_name, _ = QFileDialog.getOpenFileName(self, "打开文件", "", "Images (*.png *.xpm *.jpg)")
if file_name:
pixmap = QPixmap(file_name)
self.scene.clear()
self.scene.addPixmap(pixmap)
def save(self):
file_name, _ = QFileDialog.getSaveFileName(self, "保存文件", "", "Images (*.png *.xpm *.jpg)")
if file_name:
pixmap = self.view.grab()
pixmap.save(file_name)
def set_pen(self):
self.pen = QPen(QColor(0, 0, 0), 2)
def set_eraser(self):
self.pen = QPen(QColor(255, 255, 255), 10)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = True
self.lastPoint = event.pos()
def mouseMoveEvent(self, event):
if (event.buttons() & Qt.LeftButton) and self.drawing:
painter = QPainter(self.scene)
painter.setPen(self.pen)
painter.drawLine(self.lastPoint, event.pos())
self.lastPoint = event.pos()
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton and self.drawing:
self.drawing = False
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
这个程序使用PyQt5库创建了一个窗口,其中包含一个QGraphicsView和一个QGraphicsScene,用于显示绘制的图形。程序提供了新建、打开、保存、画笔和橡皮擦等功能,可以通过菜单栏或快捷键进行操作。您可以在窗口中使用鼠标绘制图形,支持画笔和橡皮擦两种模式。
阅读全文