【问题标题】:Python Screen Shot, only desktop background (MacBook Catalina)Python 屏幕截图,只有桌面背景(MacBook Catalina)
【发布时间】:2021-06-09 23:08:12
【问题描述】:

我正在尝试截取 tkinter 生成的 GUI 窗口的屏幕截图,但是当我截取屏幕截图时,我只得到桌面而不是 Python 窗口。

我使用新的 o/s Catalina 阅读,您必须授予终端权限,我已经这样做了,但我仍然无法获取 python 窗口。即使我进行全屏截图,我仍然看不到 Python 窗口。

https://github.com/BoboTiG/python-mss/issues/134

我尝试了几种不同的方法,但仍然无法屏幕抓取 Python 窗口。

还有其他人在使用 Catalina O/S 时遇到这些问题吗?

from PIL import Image, ImageTk
from tkinter import Tk, BOTH, Canvas, BOTH, NW, W
from tkinter.ttk import Frame, Label, Style
import pyscreenshot
import io
import os
import subprocess
import sys
import mss

top_border_height = 50
bottom_border_height = 70
screen_width = 800
screen_height = 480

video_icon_640x480_x = (800-640)/2
video_icon_640x480_y = (480-480)/2
homeicon64x64_x = 8
homeicon64x64_y = 8

root = Tk('test Screen')
root.geometry("800x480")
w = Canvas(root, width=screen_width, height=screen_height)

back_ground = ImageTk.PhotoImage(Image.open("./icon/wireframe_mode_background.png"))    
w.create_image(0, 0, image=back_ground, anchor='nw')
w.video_icon_640x480 = ImageTk.PhotoImage(Image.open("./icon/wireframe_640x480.png"))
w.create_image(video_icon_640x480_x, video_icon_640x480_y, image=w.video_icon_640x480, anchor="nw")
w.home_icon_640x480 = ImageTk.PhotoImage(Image.open("./icon/wireframe_64x64.png"))
w.create_image(homeicon64x64_x,homeicon64x64_y,image=w.home_icon_640x480, anchor="nw")
w.video_icon_640x480_1 = ImageTk.PhotoImage(Image.open("./icon/wireframe_64x64.png"))
w.create_image(728,8,image=w.video_icon_640x480_1, anchor="nw")
w.video_icon_640x480_2 = ImageTk.PhotoImage(Image.open("./icon/wireframe_64x64.png"))
w.create_image(728,80,image=w.video_icon_640x480_2, anchor="nw")

w.pack()
root.mainloop()

im = pyscreenshot.grab(bbox=(10, 10, 510, 510))  # X1,Y1,X2,Y2
im.save('screenshot.png')

with mss.mss() as sct:
   filename = sct.shot(mon=-1, output='fullscreen.png')
   print(filename)

【问题讨论】:

  • 我回答了一个类似的问题。这是link

标签: python macos screenshot screen-scraping


【解决方案1】:

答案在于 Python 版本(或者可能是 Pillow)在 MacOS 上设置所需的资源,以便在不可用时请求权限。在此之前,代码/应用程序的每个用户都必须启用 Python 启动器 才能拥有自 Catalina 以来添加的 屏幕录制 权限。我现在只需要将 MacOS 用户的这个限制/打嗝添加到我的应用程序用户手册User Manual Screenshot。我已经在Can you request the MacOS Screen Recording permission in Python提出了一个单独的具体问题@

如果这种情况发生了变化,或者您知道从 Python 应用程序中请求操作系统权限的方法,请告诉我们。上面关于回答类似问题的评论无效。它没有解决自 Catalina 以来添加的这个非常具体的 MacOS 权限问题。 (我没有足够的分数来评论评论,但可以回答这个问题;去看看。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    相关资源
    最近更新 更多