MCPcopy
hub / github.com/PySimpleGUI/PySimpleGUI / save_element_screenshot_to_disk

Method save_element_screenshot_to_disk

PySimpleGUI/PySimpleGUI.py:1849–1897  ·  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

1847
1848
1849 def save_element_screenshot_to_disk(self, filename=None):
1850 """
1851 Saves an image of the PySimpleGUI window provided into the filename provided
1852
1853 :param filename: Optional filename to save screenshot to. If not included, the User Settinds are used to get the filename
1854 :return: A PIL ImageGrab object that can be saved or manipulated
1855 :rtype: (PIL.ImageGrab | None)
1856 """
1857 global pil_import_attempted, pil_imported, PIL, ImageGrab, Image
1858
1859 if not pil_import_attempted:
1860 try:
1861 import PIL as PIL
1862 from PIL import ImageGrab
1863 from PIL import Image
1864 pil_imported = True
1865 pil_import_attempted = True
1866 except:
1867 pil_imported = False
1868 pil_import_attempted = True
1869 print('FAILED TO IMPORT PIL!')
1870 return None
1871 try:
1872 # Add a little to the X direction if window has a titlebar
1873 rect = (
1874 self.widget.winfo_rootx(), self.widget.winfo_rooty(), self.widget.winfo_rootx() + self.widget.winfo_width(), self.widget.winfo_rooty() + self.widget.winfo_height())
1875
1876 grab = ImageGrab.grab(bbox=rect)
1877 # Save the grabbed image to disk
1878 except Exception as e:
1879 # print(e)
1880 popup_error_with_traceback('Screen capture failure', 'Error happened while trying to save screencapture of an element', e)
1881 return None
1882
1883 # return grab
1884 if filename is None:
1885 folder = pysimplegui_user_settings.get('-screenshots folder-', '')
1886 filename = pysimplegui_user_settings.get('-screenshots filename-', '')
1887 full_filename = os.path.join(folder, filename)
1888 else:
1889 full_filename = filename
1890 if full_filename:
1891 try:
1892 grab.save(full_filename)
1893 except Exception as e:
1894 popup_error_with_traceback('Screen capture failure', 'Error happened while trying to save screencapture', e)
1895 else:
1896 popup_error_with_traceback('Screen capture failure', 'You have attempted a screen capture but have not set up a good filename to save to')
1897 return grab
1898
1899 def _pack_forget_save_settings(self, alternate_widget=None):
1900 """

Callers

nothing calls this directly

Calls 3

saveMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected