功能:
1. 可以在电脑当前界面进行矩形截图
2. 鼠标在滑动过程中,实时显示十字坐标并有坐标值显示
3. 坐标在不同位置会都会显示, 实时显示起止坐标和截图的长、宽
比较简单,供参考:
import tkinter as tk
import ctypes, sys, win32con, win32api
from PIL import ImageTk, ImageGrab
# pip install pywin32 -i https://mirrors.aliyun.com/pypi/simple/
# pip install pillow -i https://mirrors.aliyun.com/pypi/simple/
def screenshot_for_pc():
def catpure_image():
global press_x, press_y, last_draw, screen_w, screen_h, mouse_down, old_coords
mouse_down = False
old_coords = None
if sys.getwindowsversion().major == 10:
ctypes.windll.shcore.SetProcessDpiAwareness(2)
def capture_image_click():
screenshot_btn["text"] = "提示: 按鼠标中键可取消操作"
screenshot_btn.configure(bg = "#60C5F1")
def mouse_press(event):
global press_x, press_y, mouse_down
mouse_down = True
press_x, press_y = event.x, event.y
canvas.delete('horizontal')
canvas.delete('vertical')
canvas.delete('text')
def mouse_mv(event):
global press_x, press_y, last_draw, mouse_down, old_coords, screen_h, screen_w
x, y = event.x, event.y
if mouse_down:
canvas.delete('pos')
canvas.create_text(press_x+35, press_y-15, fill='blue', text=f' x={press_x:<4d} y={press_y:<4d}', tag='pos')
canvas.create_text(event.x+45, event.y+10, fill='blue', text=f' x={event.x:<4d} y={event.y:<4d}', tag='pos')
canvas.create_text(event.x+45, event.y+30, fill='blue', text=f' w={abs(event.x - press_x):<4d} h={abs(event.y - press_y):<4d}', tag='pos')
canvas.create_rectangle(press_x, press_y, event.x, event.y, outline='#7030A0', width=2, tag='pos')
else:
# 画十字坐标
if old_coords != None:
x1, y1 = old_coords
try:
canvas.delete('horizontal')
canvas.delete('vertical')
canvas.delete('text')
canvas.create_line(0, y, screen_w, y1, fill='blue', dash=(4, 4), width=2, tag='horizontal')
canvas.create_line(x, 0, x1, screen_h, fill='blue', dash=(4, 4), width=2, tag='vertical')
text_x = x1 + 45
text_y = y1 + 10
if x1 >= screen_w//6*5:
text_x = x1 -60
if y1 > screen_h//6*5:
text_y = y1 - 12
canvas.create_text(text_x, text_y, fill='blue', text=f' x={x1:<4d} y={y1:<4d}', tag='text')
except:
...
old_coords = x, y
def mouse_release(event):
global press_x, press_y, last_draw, mouse_down
mouse_down = False
screenshot_btn["text"] = "点一下这里开始截图Click"
screenshot_btn.configure(bg = "#D9D9D9")
top.withdraw()
img = ImageGrab.grab((press_x, press_y, event.x, event.y))
try:
img.show()
except:
win32api.MessageBox(0, '当前只能正向截图,反向截图后续完善!!!', '截图异常', win32con.MB_OK)
def mouse_mid_key(event):
global mouse_down
top.destroy()
mouse_down = False
screenshot_btn["text"] = "点一下这里开始截图Click"
screenshot_btn.configure(bg = "#D9D9D9")
top = tk.Toplevel()
top.state('zoomed')
top.overrideredirect(True)
canvas = tk.Canvas(top)
background = ImageTk.PhotoImage(ImageGrab.grab())
canvas.create_image(0, 0, anchor='nw', image=background)
canvas.bind('<Button-1>', mouse_press)
canvas.bind('<Motion>', mouse_mv)
canvas.bind('<ButtonRelease-1>', mouse_release)
canvas.bind('<Button-2>', mouse_mid_key) # 鼠标中键
canvas.config(cursor='cross')
canvas.pack(expand='YES', fill='both')
top.mainloop()
root = tk.Tk()
root.title('PC截图小工具')
screen_w = root.winfo_screenwidth()
screen_h = root.winfo_screenheight()
root.geometry(f'250x40+{screen_w - 268}+20')
root.resizable(False, False)
root.wm_attributes('-topmost', 'true')
last_draw = None
screenshot_btn = tk.Button(root, text='点一下这里开始截图Click', width=100, command=capture_image_click)
screenshot_btn.configure(bg = "#D9D9D9")
screenshot_btn.place(x=10, y=15, width=80, height=20)
screenshot_btn.pack()
root.mainloop()
ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 7)
catpure_image()
ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 4)
if __name__ == '__main__':
screenshot_for_pc()