Python 实现鼠标拖动截图
此功能由3个.py文件实现,分别为:test00.py、screenshot.py、py_tool.py;实现鼠标附近局部放大,未截图部分半透明,鼠标控制键盘精准截图,鼠标框选后自动保存截图,按下esc键退出截图;
文章来源地址https://www.toymoban.com/news/detail-626903.html
一、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()
二、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
文章来源:https://www.toymoban.com/news/detail-626903.html
到了这里,关于Python 实现鼠标拖动截图的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!