Python 实现全截屏或自定义截屏
文章目录
导入模块
首先,导入必要的模块:
import tkinter
import tkinter.filedialog
import os
from PIL import ImageGrab
from time import sleep
tkinter
:用于构建图形用户界面。tkinter.filedialog
:用于创建文件对话框,以便用户选择保存截图的位置。os
:提供与操作系统交互的功能,此处可能用于文件操作。PIL
中的ImageGrab
:用于进行屏幕截图。time
中的sleep
:用于在程序执行中暂停一段时间,这里可能用于控制截图时机。
MyCapture类
然后定义一个名为 MyCapture
的类,用于创建截图工具的界面和功能
class MyCapture:
def __init__(self, png):
self.X = tkinter.IntVar(value=0)
self.Y = tkinter.IntVar(value=0)
screenWidth = root.winfo_screenwidth()
screenHeight = root.winfo_screenheight()
self.top = tkinter.Toplevel(root, width=screenWidth, height=screenHeight)
self.top.overrideredirect(True)
self.canvas = tkinter.Canvas(self.top, bg='white', width=screenWidth, height=screenHeight)
self.image = tkinter.PhotoImage(file=png)
self.canvas.create_image(screenWidth//2, screenHeight//2, image=self.image)
MyCapture
类用于创建截图工具的界面和功能。__init__
方法初始化截图工具窗口,包括画布、图片和窗口大小等设置。
鼠标处理
def onLeftButtonDown(event):
self.X.set(event.x)
self.Y.set(event.y)
self.sel = True
self.canvas.bind('<Button-1>', onLeftButtonDown)
def onLeftButtonMove(event):
if not self.sel:
return
global lastDraw
try:
self.canvas.delete(lastDraw)
except Exception as e:
pass
lastDraw = self.canvas.create_rectangle(self.X.get(), self.Y.get(), event.x, event.y, outline='blue')
self.canvas.bind('<B1-Motion>', onLeftButtonMove)
def onLeftButtonUp(event):
self.sel = False
try:
self.canvas.delete(lastDraw)
except Exception as e:
pass
sleep(0.1)
- 这三个函数分别处理了鼠标左键按下、移动和释放事件。
onLeftButtonDown
:记录鼠标按下时的坐标。onLeftButtonMove
:在鼠标移动时绘制选中区域的矩形框。onLeftButtonUp
:在鼠标释放时完成截图操作。
按钮点击事件处理函数
def buttonCaptureClick():
root.state(‘icon’)
sleep(0.2)
filename = 'temp.png'
im = ImageGrab.grab()
im.save(filename)
im.close()
w = MyCapture(filename)
buttonCapture.wait_window(w.top)
root.state('normal')
os.remove(filename)
def fullScreenCapture():
root.state(‘icon’)
sleep(0.2)
filename = 'full_screen_temp.png'
im = ImageGrab.grab()
im.save(filename)
im.close()
pic = ImageGrab.grab()
fileName = tkinter.filedialog.asksaveasfilename(title='保存截图', filetypes=[('JPG files', '*.jpg')])
if fileName:
pic.save(fileName+'.jpg')
root.state('normal')
os.remove(filename)
buttonCaptureClick
函数处理区域截图按钮点击事件,它先将窗口最小化,然后保存当前屏幕截图到临时文件,接着创建MyCapture
实例,进行截图操作,最后恢复窗口状态并删除临时文件。fullScreenCapture
函数处理全屏截图按钮点击事件,它也是先最小化窗口,保存全屏截图到临时文件,然后通过文件对话框询问用户保存位置并保存截图,最后恢复窗口状态并删除临时文件。
主程序
root = tkinter.Tk()
root.geometry('300x100+300+200')
root.resizable(False, False)
buttonCapture = tkinter.Button(root, text='区域截图', command=buttonCaptureClick, relief=tkinter.RAISED, bd=3, font=('Helvetica', 12, 'bold'), bg='#4CAF50', fg='white')
buttonCapture.place(x=30, y=30, width=100, height=30)
buttonFullCapture = tkinter.Button(root, text='全屏截图', command=fullScreenCapture, relief=tkinter.RAISED, bd=3, font=('Helvetica', 12, 'bold'), bg='#008CBA', fg='white')
buttonFullCapture.place(x=170, y=30, width=100, height=30)
root.mainloop()
- 创建了一个 Tkinter 窗口。
- 设置窗口的大小和位置,并禁止用户调整窗口大小。
- 创建了两个按钮,分别用于触发区域截图和全屏截图。
- 启动主事件循环,使窗口能够响应用户的操作。
完整代码
import tkinter
import tkinter.filedialog
import os
from PIL import ImageGrab
from time import sleep
class MyCapture:
def __init__(self, png):
self.X = tkinter.IntVar(value=0)
self.Y = tkinter.IntVar(value=0)
screenWidth = root.winfo_screenwidth()
screenHeight = root.winfo_screenheight()
self.top = tkinter.Toplevel(root, width=screenWidth, height=screenHeight)
self.top.overrideredirect(True)
self.canvas = tkinter.Canvas(self.top, bg='white', width=screenWidth, height=screenHeight)
self.image = tkinter.PhotoImage(file=png)
self.canvas.create_image(screenWidth//2, screenHeight//2, image=self.image)
def onLeftButtonDown(event):
self.X.set(event.x)
self.Y.set(event.y)
self.sel = True
self.canvas.bind('<Button-1>', onLeftButtonDown)
def onLeftButtonMove(event):
if not self.sel:
return
global lastDraw
try:
self.canvas.delete(lastDraw)
except Exception as e:
pass
lastDraw = self.canvas.create_rectangle(self.X.get(), self.Y.get(), event.x, event.y, outline='blue')
self.canvas.bind('<B1-Motion>', onLeftButtonMove)
def onLeftButtonUp(event):
self.sel = False
try:
self.canvas.delete(lastDraw)
except Exception as e:
pass
sleep(0.1)
left, right = sorted([self.X.get(), event.x])
top, bottom = sorted([self.Y.get(), event.y])
pic = ImageGrab.grab((left+1, top+1, right, bottom))
fileName = tkinter.filedialog.asksaveasfilename(title='保存截图', filetypes=[('JPG files', '*.jpg')])
if fileName:
pic.save(fileName+'.jpg')
self.top.destroy()
self.canvas.bind('<ButtonRelease-1>', onLeftButtonUp)
self.canvas.pack(fill=tkinter.BOTH, expand=tkinter.YES)
def buttonCaptureClick():
root.state('icon')
sleep(0.2)
filename = 'temp.png'
im = ImageGrab.grab()
im.save(filename)
im.close()
w = MyCapture(filename)
buttonCapture.wait_window(w.top)
root.state('normal')
os.remove(filename)
def fullScreenCapture():
root.state('icon')
sleep(0.2)
filename = 'full_screen_temp.png'
im = ImageGrab.grab()
im.save(filename)
im.close()
pic = ImageGrab.grab()
fileName = tkinter.filedialog.asksaveasfilename(title='保存截图', filetypes=[('JPG files', '*.jpg')])
if fileName:
pic.save(fileName+'.jpg')
root.state('normal')
os.remove(filename)
root = tkinter.Tk()
root.geometry('300x100+300+200')
root.resizable(False, False)
buttonCapture = tkinter.Button(root, text='区域截图', command=buttonCaptureClick, relief=tkinter.RAISED, bd=3, font=('Helvetica', 12, 'bold'), bg='#4CAF50', fg='white')
buttonCapture.place(x=30, y=30, width=100, height=30)
buttonFullCapture = tkinter.Button(root, text='全屏截图', command=fullScreenCapture, relief=tkinter.RAISED, bd=3, font=('Helvetica', 12, 'bold'), bg='#008CBA', fg='white')
buttonFullCapture.place(x=170, y=30, width=100, height=30)
root.mainloop()
- 洗漱包旅行套装 男出差商务洗簌袋便携化妆包女用品洗护大容量
- 蓝罐(Kjeldsens)曲奇饼干礼盒 454g 丹麦原装进口 休闲零食 节日送礼福利团购
- 适用于新款车载手机支架无线器快充智能自动感应汽车
- 皇顺适配名爵MG7汽车用品MG6PRO改装饰配件MG5门槛条ZS保护贴
温馨提示 : 非特殊注明,否则均为©李联华的博客网原创文章,本站文章未经授权禁止任何形式转载;IP地址:3.144.252.58,归属地:俄亥俄州Dublin ,欢迎您的访问!
文章链接:https://www.lilianhua.com/python-implementation-of-full-or-custom-screenshots.html
文章链接:https://www.lilianhua.com/python-implementation-of-full-or-custom-screenshots.html