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

Function save_win

DemoPrograms/Demo_Save_Any_Window_As_Image.py:19–48  ·  view source on GitHub ↗

Saves a window with the title provided as a file using the provided filename. If one of them is missing, then a window is created and the information collected :param filename: :param title: :return:

(filename=None, title=None)

Source from the content-addressed store, hash-verified

17
18
19def save_win(filename=None, title=None):
20 """
21 Saves a window with the title provided as a file using the provided filename.
22 If one of them is missing, then a window is created and the information collected
23
24 :param filename:
25 :param title:
26 :return:
27 """
28 C = 7 # pixels to crop
29 if filename is None or title is None:
30 layout = [[sg.T('Choose window to save', font='Any 18')],
31 [sg.T('The extension you choose for filename will determine the image format')],
32 [sg.T('Window Title:', size=(12,1)), sg.I(title if title is not None else '', key='-T-')],
33 [sg.T('Filename:', size=(12,1)), sg.I(filename if filename is not None else '', key='-F-')],
34 [sg.Button('Ok', bind_return_key=True), sg.Button('Cancel')]]
35 event, values = sg.Window('Choose Win Title and Filename',layout).read(close=True)
36 if event != 'Ok': # if cancelled or closed the window
37 print('Cancelling the save')
38 return
39 filename, title = values['-F-'], values['-T-']
40 try:
41 fceuxHWND = win32gui.FindWindow(None, title)
42 rect = win32gui.GetWindowRect(fceuxHWND)
43 rect_cropped = (rect[0]+C, rect[1], rect[2]-C, rect[3]-C)
44 grab = ImageGrab.grab(bbox=rect_cropped)
45 grab.save(filename)
46 sg.popup('Wrote image to file:',filename)
47 except Exception as e:
48 sg.popup('Error trying to save screenshot file', e)
49
50
51if __name__ == '__main__':

Calls 2

saveMethod · 0.80
readMethod · 0.45

Tested by

no test coverage detected