以下代码使用PySide6实现一个简单的矩形截图工具,支持鼠标拖动选择区域、右键取消选择、双击确认截图。
python
复制
插入
import sys
from PySide6.QtWidgets import (QApplication, QMainWindow, QLabel,
QWidget, QVBoxLayout)
from PySide6.QtCore import Qt, QPoint, QRect
from PySide6.QtGui import QPixmap, QPainter, QPen, QColor, QScreen
class ScreenshotTool(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("截图工具")
self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
# 获取屏幕尺寸并设置全屏
screen = QApplication.primaryScreen()
self.screen_rect = screen.availableGeometry()
self.setGeometry(self.screen_rect)
# 初始化变量
self.start_point = QPoint()
self.end_point = QPoint()
self.is_dragging = False
self.captured_rect = QRect()
# 设置中心窗口
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
# 添加一个透明标签覆盖整个屏幕
self.overlay = QLabel()
self.overlay.setStyleSheet("background-color: rgba(0, 0, 0, 100);")
layout.addWidget(self.overlay)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.start_point = event.position().toPoint()
self.end_point = self.start_point
self.is_dragging = True
def mouseMoveEvent(self, event):
if self.is_dragging:
self.end_point = event.position().toPoint()
self.update()
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton and self.is_dragging:
self.is_dragging = False
self.capture_selected_area()
if event.button() == Qt.RightButton:
self.close()
def mouseDoubleClickEvent(self, event):
if event.button() == Qt.LeftButton and not self.captured_rect.isEmpty():
self.save_screenshot()
self.close()
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
# 绘制半透明覆盖层
painter.fillRect(self.rect(), QColor(0, 0, 0, 100))
# 绘制选择区域
if self.is_dragging or not self.captured_rect.isEmpty():
selection = self.get_selection_rect()
painter.setPen(QPen(Qt.white, 2, Qt.DashLine))
painter.drawRect(selection)
# 清除选择区域内的覆盖层
painter.setCompositionMode(QPainter.CompositionMode_Clear)
painter.fillRect(selection, Qt.transparent)
painter.setCompositionMode(QPainter.CompositionMode_SourceOver)
def get_selection_rect(self):
if self.is_dragging:
return QRect(self.start_point, self.end_point).normalized()
return self.captured_rect
def capture_selected_area(self):
selection = self.get_selection_rect()
if selection.width() > 10 and selection.height() > 10:
self.captured_rect = selection
self.update()
def save_screenshot(self):
screen = QApplication.primaryScreen()
screenshot = screen.grabWindow(0,
self.captured_rect.x(),
self.captured_rect.y(),
self.captured_rect.width(),
self.captured_rect.height())
screenshot.save("screenshot.png", "PNG")
print(f"截图已保存为 screenshot.png")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = ScreenshotTool()
window.show()
sys.exit(app.exec())
复制
插入
功能说明
-
全屏覆盖:创建一个半透明的全屏窗口覆盖整个屏幕,方便用户选择截图区域。
-
矩形选择:
- 左键点击并拖动创建选择矩形
- 选择区域会显示为透明,周围保持半透明覆盖
- 选择矩形边框使用白色虚线
-
操作控制:
- 右键点击取消截图操作
- 左键双击确认并保存截图
- 截图会自动保存为screenshot.png
-
截图保存:使用QScreen的grabWindow方法捕获指定区域的屏幕内容。
扩展改进建议
上篇文章我们提到了利用百度的OCR去提取图片中的文字,此文我们学会了如何写一个自定义的截图工具,这两个程序拼到一起,是不是就实现了截图复制文字的功能
817

被折叠的 条评论
为什么被折叠?



