用pyqt5写一个简单的绘图软件
时间: 2023-07-04 19:11:30 浏览: 61
好的,以下是一个简单的绘图软件的示例代码,它使用 PyQt5 库编写:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QFileDialog, QColorDialog
from PyQt5.QtGui import QPainter, QPen, QColor
from PyQt5.QtCore import Qt
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 800, 600)
self.setWindowTitle("Simple Paint")
self.image = None
self.last_point = None
self.color = Qt.black
self.pen_size = 1
self.init_ui()
def init_ui(self):
menubar = self.menuBar()
file_menu = menubar.addMenu("File")
new_action = QAction("New", self)
new_action.setShortcut("Ctrl+N")
new_action.triggered.connect(self.new_image)
file_menu.addAction(new_action)
open_action = QAction("Open", self)
open_action.setShortcut("Ctrl+O")
open_action.triggered.connect(self.open_image)
file_menu.addAction(open_action)
save_action = QAction("Save", self)
save_action.setShortcut("Ctrl+S")
save_action.triggered.connect(self.save_image)
file_menu.addAction(save_action)
color_menu = menubar.addMenu("Color")
color_action = QAction("Choose Color", self)
color_action.triggered.connect(self.choose_color)
color_menu.addAction(color_action)
pen_menu = menubar.addMenu("Pen")
pen_size_menu = pen_menu.addMenu("Pen Size")
size1_action = QAction("Size 1", self)
size1_action.triggered.connect(lambda: self.set_pen_size(1))
pen_size_menu.addAction(size1_action)
size3_action = QAction("Size 3", self)
size3_action.triggered.connect(lambda: self.set_pen_size(3))
pen_size_menu.addAction(size3_action)
size5_action = QAction("Size 5", self)
size5_action.triggered.connect(lambda: self.set_pen_size(5))
pen_size_menu.addAction(size5_action)
def new_image(self):
self.image = None
self.update()
def open_image(self):
file_path, _ = QFileDialog.getOpenFileName(self, "Open Image", "", "Images (*.png *.jpg *.bmp)")
if file_path:
self.image = file_path
self.update()
def save_image(self):
if self.image:
file_path, _ = QFileDialog.getSaveFileName(self, "Save Image", "", "Images (*.png *.jpg *.bmp)")
if file_path:
painter = QPainter(self.image)
self.draw(painter)
self.image.save(file_path)
def choose_color(self):
self.color = QColorDialog.getColor(self.color, self)
if not self.color.isValid():
self.color = Qt.black
def set_pen_size(self, size):
self.pen_size = size
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.last_point = event.pos()
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
painter = QPainter(self.image)
self.draw(painter, event.pos())
self.update()
self.last_point = event.pos()
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(QPen(self.color, self.pen_size, Qt.SolidLine))
if self.image:
painter.drawImage(0, 0, self.image)
def draw(self, painter, end_point=None):
if not self.image:
self.image = QImage(self.width(), self.height(), QImage.Format_RGB32)
self.image.fill(Qt.white)
painter.drawLine(self.last_point, end_point or self.last_point)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
这个简单的绘图软件可以实现以下功能:
1. 新建、打开、保存图片;
2. 选择画笔颜色和大小;
3. 用鼠标绘制图形。
希望能帮到你!
阅读全文