MCPcopy Index your code
hub / github.com/PySimpleGUI/PySimpleGUI / save_window_screenshot_to_disk

Method save_window_screenshot_to_disk

PySimpleGUI/PySimpleGUI.py:12305–12367  ·  view source on GitHub ↗

Saves an image of the PySimpleGUI window provided into the filename provided :param filename: Optional filename to save screenshot to. If not included, the User Settinds are used to get the filename :return: A PIL ImageGrab object that can be saved or

(self, filename=None)

Source from the content-addressed store, hash-verified

12303 self.TKRightClickMenu.grab_release()
12304
12305 def save_window_screenshot_to_disk(self, filename=None):
12306 """
12307 Saves an image of the PySimpleGUI window provided into the filename provided
12308
12309 :param filename: Optional filename to save screenshot to. If not included, the User Settinds are used to get the filename
12310 :return: A PIL ImageGrab object that can be saved or manipulated
12311 :rtype: (PIL.ImageGrab | None)
12312 """
12313 global pil_import_attempted, pil_imported, PIL, ImageGrab, Image
12314
12315 if not pil_import_attempted:
12316 try:
12317 import PIL as PIL
12318 from PIL import ImageGrab
12319 from PIL import Image
12320 pil_imported = True
12321 pil_import_attempted = True
12322 except:
12323 pil_imported = False
12324 pil_import_attempted = True
12325 print('FAILED TO IMPORT PIL!')
12326 return None
12327 try:
12328 # Get location of window to save
12329 pos = self.current_location()
12330 # Add a little to the X direction if window has a titlebar
12331 if not self.NoTitleBar:
12332 pos = (pos[0] + 7, pos[1])
12333 # Get size of wiondow
12334 size = self.current_size_accurate()
12335 # Get size of the titlebar
12336 titlebar_height = self.TKroot.winfo_rooty() - self.TKroot.winfo_y()
12337 # Add titlebar to size of window so that titlebar and window will be saved
12338 size = (size[0], size[1] + titlebar_height)
12339 if not self.NoTitleBar:
12340 size_adjustment = (2, 1)
12341 else:
12342 size_adjustment = (0, 0)
12343 # Make the "Bounding rectangle" used by PLK to do the screen grap "operation
12344 rect = (pos[0], pos[1], pos[0] + size[0] + size_adjustment[0], pos[1] + size[1] + size_adjustment[1])
12345 # Grab the image
12346 grab = ImageGrab.grab(bbox=rect)
12347 # Save the grabbed image to disk
12348 except Exception as e:
12349 # print(e)
12350 popup_error_with_traceback('Screen capture failure', 'Error happened while trying to save screencapture', e)
12351
12352 return None
12353 # return grab
12354 if filename is None:
12355 folder = pysimplegui_user_settings.get('-screenshots folder-', '')
12356 filename = pysimplegui_user_settings.get('-screenshots filename-', '')
12357 full_filename = os.path.join(folder, filename)
12358 else:
12359 full_filename = filename
12360 if full_filename:
12361 try:
12362 grab.save(full_filename)

Callers 1

readMethod · 0.95

Calls 5

current_locationMethod · 0.95
current_size_accurateMethod · 0.95
saveMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected