我正在尝试使用 python 截取特定区域的屏幕截图。我写了这段代码:import pyscreenshotfrom pynput.mouse import Listenerx=1y=1def on_click(x1, y1, button, pressed): global x,y x = x1 y = y1def on_release(x2,y2, button, pressed): global x,y im = pyscreenshot.grab(x,y,x2,y2) im.save("hello.png")#Collect events until releasedwith Listener( on_click=on_click, on_release=on_release) as listener: listener.join()这里的问题是它不输出任何内容。请帮助
1 回答
偶然的你
TA贡献1841条经验 获得超3个赞
我给你写了一个代码。在我这边效果很好。请在运行我的代码后让我知道您的意见。
import pyscreenshot
from pynput.mouse import Listener, Button
global x0, y0
def on_click(x1, y1, button, pressed):
global x0, y0
if button == Button.left and pressed:
x0, y0 = x1, y1
if button == Button.left and not pressed:
try:
im = pyscreenshot.grab(bbox=(x0, y0, x1, y1))
im.save("hello.png")
print('Screenshot was taken.')
return False
except:
pass
return True
# Collect events until released
with Listener(
on_click=on_click) as listener:
try:
listener.join()
except:
pass
添加回答
举报
0/150
提交
取消