当前位置:   article > 正文

Python 实现鼠标拖动截图_python 鼠标 拖拽 截图

python 鼠标 拖拽 截图

Python 实现鼠标拖动截图

此功能由3个.py文件实现,分别为:test00.py、screenshot.py、py_tool.py;实现鼠标附近局部放大,未截图部分半透明,鼠标控制键盘精准截图,鼠标框选后自动保存截图,按下esc键退出截图;
在这里插入图片描述

一、test00.py 用于测试

import tkinter as tk

from common import py_tool
from common.screenshot import Screenshot

scale = py_tool.get_screen_scale_rate()
py_tool.eliminate_scaling_interference()
top = tk.Tk()

Screenshot(top, scale)

top.mainloop()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

二、screenshot.py 实现截图

# coding:utf-8

import time
import tkinter as tk
import keyboard
from PIL import ImageGrab, ImageTk, Image

from common import py_tool


# 截图类
class Screenshot:
    def __init__(self, top, scale):
        """---------------------------------------------------系统设置"""
        # 获取屏幕缩放比例;
        self.scale = scale
        # 排除缩放干扰;
        py_tool.eliminate_scaling_interference()
        # 初始化关闭窗口标志;
        self.win_close = 0
        "---------------------------------------------------初始窗口"
        # 最小化主窗口;
        top.iconify()
        # 实例化主窗口;
        self.top_screenshot = tk.Toplevel(top)
        # 不显示标题栏;
        self.top_screenshot.overrideredirect(True)
        # 窗口长和宽;
        self.width_win = self.top_screenshot.winfo_screenwidth()
        self.height_win = self.top_screenshot.winfo_screenheight()
        # 设置主窗口大小和位置;
        self.top_screenshot.geometry(str(int(self.width_win)) + 'x' + str(int(self.height_win)) + '+' +
                                     str(int((self.top_screenshot.winfo_screenwidth() - self.width_win) / 2)) + '+' +
                                     str(int((self.top_screenshot.winfo_screenheight() - self.height_win) / 2)))
        # 设置窗口背景颜色,鼠标样式;
        self.top_screenshot.config(bg='', cursor='crosshair')
        "---------------------------------------------------全屏截图"
        # 实时放大会把雾窗口和截图边框放大,不美观,速度慢,放大已完成截图的图片;
        time.sleep(0.5)
        # 全屏截图;
        self.img_screenshot = ImageGrab.grab(
            (0, 0, self.top_screenshot.winfo_screenwidth(), self.top_screenshot.winfo_screenheight()))
        # 保存截图;
        self.img_screenshot.save('test00.png')
        # 打开截图;
        self.img_screenshot = Image.open('test00.png')
        "---------------------------------------------------窗口边框"
        # 初始化边框画布边框宽度;
        self.canvas_frame_frame_width = 1 * self.scale
        # 创建上边框画布;
        canvas_frame_up = tk.Canvas(self.top_screenshot, width=self.width_win, height=self.canvas_frame_frame_width,
                                    bg='yellow',
                                    highlightthickness=self.canvas_frame_frame_width,
                                    highlightbackground='yellow')
        # 设置上边框画布位置;
        canvas_frame_up.place(relx=0.5, rely=0, anchor=tk.CENTER)
        canvas_frame_down = tk.Canvas(self.top_screenshot, width=self.width_win,
                                      height=self.canvas_frame_frame_width + 1,
                                      bg='yellow',
                                      highlightthickness=self.canvas_frame_frame_width,
                                      highlightbackground='yellow')
        canvas_frame_down.place(relx=0.5, rely=1, anchor=tk.CENTER)
        canvas_frame_left = tk.Canvas(self.top_screenshot, width=self.canvas_frame_frame_width, height=
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/896669
推荐阅读
相关标签
  

闽ICP备14008679号