程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Using Python to implement a simple screenshot tool

編輯:Python

This is a screenshot gadget that colleagues want to display after the screenshot is finished and always precede during work ( That is, it will not be overwritten by other programs ) Go straight to the code :

# # -*- coding: utf-8 -*-import tkinter as tkimport pyautoguiimport tkinterfrom PIL import ImageTkfrom PIL import Imageroot = tk.Tk()root.wm_attributes('-topmost', 1)root.overrideredirect(True) # Hide the title bar of the window # root.attributes("-alpha", 0.3) # Window transparency 70 %root.attributes("-alpha", 0.4) # Window transparency 60 %# root.geometry("300x200+10+10") # Set the window size and position root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))root.configure(bg="blue")# When toolbar canvas = tk.Canvas(root)canvas.configure(width=300)canvas.configure(height=100)canvas.configure(bg="yellow")canvas.configure(highlightthickness=0) # Highlight thickness canvas.place(x=(root.winfo_screenwidth() - 500), y=(root.winfo_screenheight() - 300))canvas.create_text(150, 50, font='Arial -20 bold', text='ESC sign out , Pretend toolbar ')# To create a 1 individual Canvas Used to circle cv = tk.Canvas(root)x, y = 0, 0xstart, ystart = 0, 0def 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)# Press the left mouse button 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)# Press the left mouse button and move 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)# Release the left mouse button 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')# sign out def sys_out(even): root.destroy() func()# The binding event canvas.bind("<B1-Motion>", move)# Bind events to Esc key , When pressed Esc Key will call sys_out function , Pop-up dialog box 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()

The specific usage is to run the program 、 Selected area ( Multiple selections are supported , The last time is mainly selected )、 Press esc Complete the screenshot and pop up the screenshot photo form always in front .

I packed it and sent it to my colleagues, who said it was easy to use , Just a shortcut key .

It's also very simple , Just pack it into exe After the program, right-click the shortcut key set in the properties ~

This is about using Python This is the end of the article on implementing a simple screenshot tool , More about Python Please search the previous articles of SDN or continue to browse the relevant articles below. I hope you will support SDN more in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved