下面是关于“PyQt5--创建绘画应用”的完整使用攻略。
1. 准备工作
在开始创建绘画应用之前,需要进行一些准备工作,分别是安装PyQt5和导入相关模块。
1.1 安装PyQt5
可以使用pip命令来安装PyQt5,具体命令如下:
pip install PyQt5
1.2 导入相关模块
在创建绘画应用时,需要使用到以下几个模块:
import sys
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtGui import QImage, QPainter, QPen
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QFileDialog
2. 创建绘画应用
在准备工作完成之后,就可以创建绘画应用了,主要分为以下几个部分。
2.1 继承QMainWindow类
需要从QMainWindow类继承一个新类DrawingApp,继承过程中需要重写初始化方法和重载绘图事件方法。
class DrawingApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("绘画应用")
self.setGeometry(100, 100, 800, 600)
# 初始化画布,填充为白色
self.image = QImage(self.size(), QImage.Format_RGB32)
self.image.fill(Qt.white)
# 记录画笔位置和画笔状态
self.last_point = QPoint()
self.drawing = False
# 初始化画笔样式
self.pen = QPen(Qt.black)
self.pen.setWidth(2)
def paintEvent(self, event):
painter = QPainter(self)
painter.drawImage(self.rect(), self.image, self.image.rect())
2.2 创建菜单栏和工具栏
需要在创建的类中添加菜单栏和工具栏,分别添加文件菜单和画笔颜色工具。
class DrawingApp(QMainWindow):
def __init__(self):
super().__init__()
# ...
# 创建菜单栏
main_menu = self.menuBar()
file_menu = main_menu.addMenu("文件")
# 添加菜单项
open_file_action = QAction("打开", self)
open_file_action.setShortcut("Ctrl+O")
open_file_action.triggered.connect(self.open_file_dialog)
save_file_action = QAction("保存", self)
save_file_action.setShortcut("Ctrl+S")
save_file_action.triggered.connect(self.save_file_dialog)
file_menu.addAction(open_file_action)
file_menu.addAction(save_file_action)
# 创建工具栏
toolbar = self.addToolBar("画笔颜色")
toolbar.setMovable(False)
# 添加工具项
color_action = QAction("画笔颜色", self)
color_action.triggered.connect(self.color_picker)
toolbar.addAction(color_action)
2.3 添加绘图事件处理函数
当鼠标按下并移动时,需要记录鼠标位置,以此来进行绘图。
class DrawingApp(QMainWindow):
# ...
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.last_point = event.pos()
self.drawing = True
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = False
def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton and self.drawing:
painter = QPainter(self.image)
painter.setPen(self.pen)
painter.drawLine(self.last_point, event.pos())
self.last_point = event.pos()
self.update()
2.4 添加文件读写和画笔颜色选择函数
class DrawingApp(QMainWindow):
# ...
def open_file_dialog(self):
file_path, _ = QFileDialog.getOpenFileName(self,
"请选择一个图像文件", ".", "图像文件 (*.jpg *.png *.bmp *.gif)")
if file_path:
self.image.load(file_path)
self.update()
def save_file_dialog(self):
file_path, _ = QFileDialog.getSaveFileName(self,
"请选择一个图像文件", ".", "图像文件 (*.jpg *.png *.bmp *.gif)")
if file_path:
self.image.save(file_path)
def color_picker(self):
chosen_color = QColorDialog.getColor()
if chosen_color.isValid():
self.pen.setColor(chosen_color)
3. 示例说明
下面给出两个完整的示例,分别展示如何打开图片和如何使用不同颜色的画笔绘图。
3.1 打开图片
import sys
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtGui import QImage, QPainter, QPen
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QFileDialog
class DrawingApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("绘画应用")
self.setGeometry(100, 100, 800, 600)
self.image = QImage(self.size(), QImage.Format_RGB32)
self.image.fill(Qt.white)
self.last_point = QPoint()
self.drawing = False
self.pen = QPen(Qt.black)
self.pen.setWidth(2)
main_menu = self.menuBar()
file_menu = main_menu.addMenu("文件")
open_file_action = QAction("打开", self)
open_file_action.setShortcut("Ctrl+O")
open_file_action.triggered.connect(self.open_file_dialog)
save_file_action = QAction("保存", self)
save_file_action.setShortcut("Ctrl+S")
save_file_action.triggered.connect(self.save_file_dialog)
file_menu.addAction(open_file_action)
file_menu.addAction(save_file_action)
def open_file_dialog(self):
file_path, _ = QFileDialog.getOpenFileName(self,
"请选择一个图像文件", ".", "图像文件 (*.jpg *.png *.bmp *.gif)")
if file_path:
self.image.load(file_path)
self.update()
def save_file_dialog(self):
file_path, _ = QFileDialog.getSaveFileName(self,
"请选择一个图像文件", ".", "图像文件 (*.jpg *.png *.bmp *.gif)")
if file_path:
self.image.save(file_path)
def paintEvent(self, event):
painter = QPainter(self)
painter.drawImage(self.rect(), self.image, self.image.rect())
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.last_point = event.pos()
self.drawing = True
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = False
def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton and self.drawing:
painter = QPainter(self.image)
painter.setPen(self.pen)
painter.drawLine(self.last_point, event.pos())
self.last_point = event.pos()
self.update()
app = QApplication(sys.argv)
window = DrawingApp()
window.show()
sys.exit(app.exec_())
在菜单栏中选择“文件”-“打开”菜单项,即可打开本地的jpg、png、bmp、gif等格式的图片。
3.2 使用不同颜色的画笔绘图
import sys
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtGui import QImage, QPainter, QPen, QColor
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QFileDialog, QColorDialog
class DrawingApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("绘画应用")
self.setGeometry(100, 100, 800, 600)
self.image = QImage(self.size(), QImage.Format_RGB32)
self.image.fill(Qt.white)
self.last_point = QPoint()
self.drawing = False
self.pen = QPen(Qt.black)
self.pen.setWidth(2)
main_menu = self.menuBar()
file_menu = main_menu.addMenu("文件")
open_file_action = QAction("打开", self)
open_file_action.setShortcut("Ctrl+O")
open_file_action.triggered.connect(self.open_file_dialog)
save_file_action = QAction("保存", self)
save_file_action.setShortcut("Ctrl+S")
save_file_action.triggered.connect(self.save_file_dialog)
file_menu.addAction(open_file_action)
file_menu.addAction(save_file_action)
toolbar = self.addToolBar("画笔颜色")
toolbar.setMovable(False)
color_action = QAction("画笔颜色", self)
color_action.triggered.connect(self.color_picker)
toolbar.addAction(color_action)
def open_file_dialog(self):
file_path, _ = QFileDialog.getOpenFileName(self,
"请选择一个图像文件", ".", "图像文件 (*.jpg *.png *.bmp *.gif)")
if file_path:
self.image.load(file_path)
self.update()
def save_file_dialog(self):
file_path, _ = QFileDialog.getSaveFileName(self,
"请选择一个图像文件", ".", "图像文件 (*.jpg *.png *.bmp *.gif)")
if file_path:
self.image.save(file_path)
def color_picker(self):
chosen_color = QColorDialog.getColor()
if chosen_color.isValid():
new_pen = QPen(chosen_color)
new_pen.setWidth(2)
self.pen = new_pen
def paintEvent(self, event):
painter = QPainter(self)
painter.drawImage(self.rect(), self.image, self.image.rect())
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.last_point = event.pos()
self.drawing = True
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = False
def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton and self.drawing:
painter = QPainter(self.image)
painter.setPen(self.pen)
painter.drawLine(self.last_point, event.pos())
self.last_point = event.pos()
self.update()
app = QApplication(sys.argv)
window = DrawingApp()
window.show()
sys.exit(app.exec_())
在工具栏中选择“画笔颜色”按钮,即可选择不同的画笔颜色。
至此,关于“PyQt5--创建绘画应用”的完整使用攻略已经介绍完毕,希望能对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:PyQt5–创建绘画应用 - Python技术站