Python 實(shí)現(xiàn)鼠標(biāo)拖動(dòng)截圖
此功能由3個(gè).py文件實(shí)現(xiàn),分別為:test00.py、screenshot.py、py_tool.py;實(shí)現(xiàn)鼠標(biāo)附近局部放大,未截圖部分半透明,鼠標(biāo)控制鍵盤精準(zhǔn)截圖,鼠標(biāo)框選后自動(dòng)保存截圖,按下esc鍵退出截圖;文章來源地址http://www.zghlxwxcb.cn/news/detail-626903.html
一、test00.py 用于測(cè)試
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 實(shí)現(xiàn)截圖
# 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):
"""---------------------------------------------------系統(tǒng)設(shè)置"""
# 獲取屏幕縮放比例;
self.scale = scale
# 排除縮放干擾;
py_tool.eliminate_scaling_interference()
# 初始化關(guān)閉窗口標(biāo)志;
self.win_close = 0
"---------------------------------------------------初始窗口"
# 最小化主窗口;
top.iconify()
# 實(shí)例化主窗口;
self.top_screenshot = tk.Toplevel(top)
# 不顯示標(biāo)題欄;
self.top_screenshot.overrideredirect(True)
# 窗口長(zhǎng)和寬;
self.width_win = self.top_screenshot.winfo_screenwidth()
self.height_win = self.top_screenshot.winfo_screenheight()
# 設(shè)置主窗口大小和位置;
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)))
# 設(shè)置窗口背景顏色,鼠標(biāo)樣式;
self.top_screenshot.config(bg='', cursor='crosshair')
"---------------------------------------------------全屏截圖"
# 實(shí)時(shí)放大會(huì)把霧窗口和截圖邊框放大,不美觀,速度慢,放大已完成截圖的圖片;
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')
"---------------------------------------------------窗口邊框"
# 初始化邊框畫布邊框?qū)挾?
self.canvas_frame_frame_width = 1 * self.scale
# 創(chuàng)建上邊框畫布;
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')
# 設(shè)置上邊框畫布位置;
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
文章來源:http://www.zghlxwxcb.cn/news/detail-626903.html
到了這里,關(guān)于Python 實(shí)現(xiàn)鼠標(biāo)拖動(dòng)截圖的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!