0

I am trying to take a screen shot of a GUI window generated by tkinter, but when I take a screen shot I only get the desktop and not the Python window.

I read with the new o/s Catalina you have to give the terminal permissions, which I have done but still I cannot grab the python window. Even if I do a full screen shot I still cannot see the Python window.

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

I have tried several different ways, but still cannot get to screen grab the Python window.

Does any one else have these problems with 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)

enter image description here

Brendon
  • 123
  • 9
  • 1
    I answered a similier question. Here's the [link](https://stackoverflow.com/questions/40809729/can-python-get-the-screen-shot-of-a-specific-window). – tinus Oct 22 '20 at 19:35

1 Answers1

0

The answer lies in the Python release (or maybe Pillow) setting up the needed resource when on MacOS so the permission is requested when not available. Until that happens, each user of the code / application will have to enable the Python Launcher to have the Screen Recording permission added since Catalina. I simply had to add this limitation / hiccup for MacOS users to my applications' user manual for now User Manual Screenshot. I have made a separate, specific question about this at Can you request the MacOS Screen Recording permission in Python

If this changes or you know a way to request an OS permission from within a Python application, please let us know. The comment above about answering a similar question is not valid. It is not addressing this very specific MacOS permission issue added since Catalina. (I do not have enough points to comment on the comment but could answer the question; go figure.)

  • did you end up finding a solution to asking for permissions for a python code? – nyxaria Aug 01 '23 at 20:32
  • @nyxaria no. Someone has to write a cython link with the objective C code indicated in [the question linked in the comment above](https://stackoverflow.com/questions/67906393/can-you-request-the-macos-screen-recording-permission-in-python). That could either be a separate module that gets imported or embedded in the screenshot libraries. As [pyscreenshot states it is pure python, they will not add it](https://github.com/ponty/pyscreenshot/issues/87). Note that you cannot do it at the shell level as it is the Python interpreter executable that must have the permission set. – SurplusGadgets Aug 03 '23 at 14:53