Python3使用PyQt5制作简单的画板/手写板
时间: 2024-01-08 17:04:56 浏览: 111
好的,我可以帮你解答这个问题。下面是一个简单的Python3程序,使用PyQt5模块制作一个画板/手写板。
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QAction, QMenu, QColorDialog, QFileDialog
from PyQt5.QtGui import QPainter, QPen, QColor, QImage
from PyQt5.QtCore import Qt, QPoint
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 800, 600)
self.setWindowTitle("画板")
self.image = QImage(self.size(), QImage.Format_RGB32)
self.image.fill(Qt.white)
self.drawing = False
self.brush_size = 2
self.brush_color = Qt.black
self.last_point = QPoint()
main_menu = self.menuBar()
file_menu = main_menu.addMenu("文件")
brush_menu = main_menu.addMenu("画笔")
color_menu = main_menu.addMenu("颜色")
save_action = QAction("保存", self)
save_action.setShortcut("Ctrl+S")
save_action.triggered.connect(self.save)
file_menu.addAction(save_action)
clear_action = QAction("清空", self)
clear_action.setShortcut("Ctrl+C")
clear_action.triggered.connect(self.clear)
file_menu.addAction(clear_action)
size_action_1 = QAction("小", self)
size_action_1.setShortcut("Ctrl+1")
size_action_1.triggered.connect(self.set_small_brush)
brush_menu.addAction(size_action_1)
size_action_2 = QAction("中", self)
size_action_2.setShortcut("Ctrl+2")
size_action_2.triggered.connect(self.set_medium_brush)
brush_menu.addAction(size_action_2)
size_action_3 = QAction("大", self)
size_action_3.setShortcut("Ctrl+3")
size_action_3.triggered.connect(self.set_large_brush)
brush_menu.addAction(size_action_3)
color_action = QAction("选择颜色", self)
color_action.setShortcut("Ctrl+Shift+C")
color_action.triggered.connect(self.choose_color)
color_menu.addAction(color_action)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = True
self.last_point = event.pos()
def mouseMoveEvent(self, event):
if (event.buttons() & Qt.LeftButton) and self.drawing:
painter = QPainter(self.image)
painter.setPen(QPen(self.brush_color, self.brush_size, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
painter.drawLine(self.last_point, event.pos())
self.last_point = event.pos()
self.update()
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = False
def paintEvent(self, event):
painter = QPainter(self)
painter.drawImage(self.rect(), self.image, self.image.rect())
def save(self):
file_name, _ = QFileDialog.getSaveFileName(self, "保存画板", "", "PNG(*.png);;JPEG(*.jpg *.jpeg);;All Files(*.*)")
if file_name == "":
return
self.image.save(file_name)
def clear(self):
self.image.fill(Qt.white)
self.update()
def set_small_brush(self):
self.brush_size = 2
def set_medium_brush(self):
self.brush_size = 5
def set_large_brush(self):
self.brush_size = 10
def choose_color(self):
color = QColorDialog.getColor()
if color.isValid():
self.brush_color = color
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
```
你可以将这个程序保存为一个.py文件,然后使用Python3解释器执行它。当你打开这个程序时,你将看到一个带有菜单栏和一个空白区域的窗口。菜单栏中有文件、画笔和颜色三个菜单,你可以在这里选择不同的选项来操作画板。在画板上,你可以使用鼠标左键绘制图形,使用菜单栏中的选项来选择画笔的大小和颜色,以及保存或清空画板。
希望这个简单的例子能够帮到你!
阅读全文