这篇文章主要讲解了“怎么用Python实现一个简易的截图工具”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用Python实现一个简易的截图工具”吧!
代码:
# # -*- coding: utf-8 -*-import tkinter as tkimport pyautoguiimport tkinterfrom PIL import ImageTkfrom PIL import Image root = tk.Tk()root.wm_attributes('-topmost', 1) root.overrideredirect(True) # 隐藏窗口的栏# root.attributes("-alpha", 0.3) # 窗口透明度70 %root.attributes("-alpha", 0.4) # 窗口透明度60 %# root.geometry("300x200+10+10") # 设置窗口大小与位置root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))root.configure(bg="blue") # 当工具条canvas = tk.Canvas(root)canvas.configure(width=300)canvas.configure(height=100)canvas.configure(bg="yellow")canvas.configure(highlightthickness=0) # 高亮厚度canvas.place(x=(root.winfo_screenwidth() - 500), y=(root.winfo_screenheight() - 300))canvas.create_text(150, 50, font='Arial -20 bold', text='ESC退出,假装工具条') # 再创建1个Canvas用于圈选cv = tk.Canvas(root)x, y = 0, 0xstart, ystart = 0, 0 def move(event): global x, y, xstart, ystart new_x = (event.x - x) + canvas.winfo_x() new_y = (event.y - y) + canvas.winfo_y() s = "300x200+" + str(new_x) + "+" + str(new_y) canvas.place(x=new_x - xstart, y=new_y - ystart) print("s = ", s) print(root.winfo_x(), root.winfo_y()) print(event.x, event.y) # 鼠标左键按下def button_1(event): global x, y, xstart, ystart x, y = event.x, event.y xstart, ystart = event.x, event.y print("event.x, event.y = ", event.x, event.y) xstart, ystart = event.x, event.y cv.configure(height=1) cv.configure(width=1) cv.place(x=event.x, y=event.y) # 鼠标左键按下并移动def b1_Motion(event): global x, y x, y = event.x, event.y print("event.x, event.y = ", event.x, event.y) cv.configure(height=event.y - ystart) cv.configure(width=event.x - xstart) # 鼠标左键松开def buttonRelease_1(event): global x, y, xstart, ystart x, y = event.x, event.y print("event.x, event.y = ", event.x, event.y) Pstart = [0, 0] cv.place_forget() img = pyautogui.screenshot(region=[xstart, ystart, x - xstart, y - ystart]) # x,y,w,h img.save('screenshot.png') # 退出def sys_out(even): root.destroy() func() # 绑定事件canvas.bind("<B1-Motion>", move)# 绑定事件到Esc键,当按下Esc键就会调用sys_out函数,弹出对话框root.bind('<Escape>', sys_out)root.bind("<Button-1>", button_1)root.bind("<B1-Motion>", b1_Motion)root.bind("<ButtonRelease-1>", buttonRelease_1)img_png = Nonedef func(): root1 = tk.Tk() root1.wm_attributes('-topmost', 1) img_open = Image.open("screenshot.png") global img_png img_png = ImageTk.PhotoImage(img_open) label_img = tk.Label(root1, image = img_png) label_img.pack() root.mainloop()
具体用法是运行程序、选中区域(支持多次选定,以最后一次选定为主)、按esc完成截图同时弹出永远前置的截图照片窗体。
感谢各位的阅读,以上就是“怎么用Python实现一个简易的截图工具”的内容了,经过本文的学习后,相信大家对怎么用Python实现一个简易的截图工具这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!