昨天发现,打开软件的菜单然后用QQ和微信的快捷键组合截图,根本截不了,一旦用到ALT键,菜单它就缩回去了。而且也不能连续截图并自动保存,于是用python写了这个工具解决这些问题。没有过多花里胡俏的功能,纯全屏截图,所以主窗口就别出来了,给它一个图标刷一下存在感就行了。
运行后会在任务栏托盘处生成一个图标,点击键盘上那个Print Screen键就能自动截图并保存在桌面上一个叫“截图保存”的目录里。PS:安装了360全家桶的请忽略本帖,毕竟pyinstaller打包一句代码print“DF35AG一出,世界的眼神都变清澈了!”,它也报毒。代码附在最后,可以自行看码,有注释了。
下载链接:https://www.123684.com/s/EG5A-fY0AH
[Python] 纯文本查看 复制代码 import pystray
from PIL import Image
import tkinter as tk
from pynput import keyboard # 导入键盘监听模块
import pyautogui
from datetime import datetime
import os
import threading
import sys
# 图标文件路径
icon_path = r'C:\Program Files (x86)\Autodesk\Autodesk Desktop App\AdAppMgr.ico'
# 创建或获取截图保存的文件夹
def create_screenshot_folder():
desktop = os.path.join(os.path.expanduser("~"), 'Desktop')
screenshot_dir = os.path.join(desktop, "截图保存")
if not os.path.exists(screenshot_dir):
os.makedirs(screenshot_dir)
return screenshot_dir
# 捕获屏幕截图,并根据当前时间命名
def capture_and_save_screenshot(screenshot_dir):
now = datetime.now()
timestamp = now.strftime("%Y%m%d_%H%M%S")
filename = f"{timestamp}.png"
screenshot_path = os.path.join(screenshot_dir, filename)
pyautogui.screenshot(screenshot_path)
print(f"截图已保存到 {screenshot_path}")
# 监听键盘事件
class KeyboardListenerThread(threading.Thread):
def __init__(self, stop_event):
super().__init__()
self.stop_event = stop_event
def run(self):
with keyboard.Listener(on_press=lambda k: on_press(k, self.stop_event)) as listener:
self.stop_event.wait()
listener.stop()
def on_press(key, stop_event):
try:
if key == keyboard.Key.print_screen:
capture_and_save_screenshot(screenshot_dir)
except AttributeError:
pass
# 创建托盘图标
def create_icon(icon_image, stop_event):
def exit_app(icon, item):
stop_event.set()
print("程序正在退出...")
icon.stop() # 停止托盘图标
def take_screenshot(icon, item):
capture_and_save_screenshot(screenshot_dir)
menu = pystray.Menu(
pystray.MenuItem('退出', exit_app),
pystray.MenuItem('截图', take_screenshot)
)
if icon_image is not None:
icon = pystray.Icon("name", icon_image, menu=menu)
else:
icon = pystray.Icon("name", menu=menu)
icon.run(on_activate)
def on_activate(icon, item=None):
print("托盘图标激活。")
print(f"Current icon state: {icon.visible}") # 检查托盘图标是否可见
icon.visible = True # 明确设置托盘图标为可见
icon.update_menu()
print(f"Updated icon state: {icon.visible}") # 再次检查托盘图标是否可见
# 主函数
def main():
global screenshot_dir
screenshot_dir = create_screenshot_folder()
# 加载图标文件
try:
icon_image = Image.open(icon_path)
except IOError:
print("Failed to load the icon file.")
icon_image = None
# 设置停止事件
stop_event = threading.Event()
# 启动托盘图标
icon_thread = threading.Thread(target=create_icon, args=(icon_image, stop_event))
icon_thread.start()
# 启动键盘监听线程
listener_thread = KeyboardListenerThread(stop_event)
listener_thread.start()
# 等待所有线程完成
icon_thread.join()
listener_thread.join()
if __name__ == "__main__":
main()
|