简体   繁体   English

使用 ImageGrab.grab() 时发生的奇怪事情

[英]Weird things happening when using ImageGrab.grab()

I have some code:我有一些代码:

from tkinter import *
from tkinter import *
from PIL import Image, ImageGrab

root = Tk()


def capture():
    x0 = canvas.winfo_rootx()
    y0 = canvas.winfo_rooty()
    x1 = x0 + canvas.winfo_width()
    y1 = y0 + canvas.winfo_height()

    im = ImageGrab.grab((x0, y0, x1, x2))
    im.save('mypic.png')  # Can also say im.show() to display it


canvas = Canvas(root, bg='red')
canvas.pack(padx=10, pady=10)

e = Entry(root)

canvas.create_window(canvas.canvasx(100), canvas.canvasy(100), window=e)

Button(root, text='Click a pic', command=capture).pack()

root.mainloop()

It makes mypic:它使mypic:

在此处输入图像描述

Which is part of my background But i want it to make:这是我背景的一部分但我想让它: 在此处输入图像描述

Which is a screenshot of my screen.这是我的屏幕截图。

I tried changing other parameters like include_layeder_windows , but it didn't change anything.我尝试更改其他参数,例如include_layeder_windows ,但它没有改变任何东西。 My problem is that grab() returns my background instead of my screen.我的问题是 grab() 返回我的背景而不是我的屏幕。

It seems you're using Mac OS.看来您使用的是 Mac OS。 In Mac, an application by default cannot grab contents of other application's window.在 Mac 中,一个应用程序默认不能抓取其他应用程序的 window 的内容。 When trying to do so, it will grab the desktop background only.尝试这样做时,它只会抓取桌面背景。 This could perhaps be observed eg when sharing screen over eg Zoom, without giving Zoom proper permissions.例如,在通过 Zoom 共享屏幕时,可能会观察到这种情况,而没有给予 Zoom 适当的权限。

To fix that, you need to go to "System Preferences" > "Privacy" > "Privacy" > "Screen Recording" and add your application there.要解决这个问题,您需要 go 到“系统偏好设置”>“隐私”>“隐私”>“屏幕录制”并在那里添加您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM