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

Function main

DemoPrograms/Demo_Save_Windows_As_Images.py:103–144  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

101
102
103def main():
104 layout = [[sg.Text('Window Snapshot', key='-T-', font='Any 20', justification='c')],
105 [sg.Text('Choose one or more window titles from list')],
106 [sg.Listbox(values=[' '], size=(50, 20), select_mode=sg.SELECT_MODE_EXTENDED, font=('Courier', 12), key='-PROCESSES-')],
107 [sg.Checkbox('Show only Python programs', default=True, key='-PYTHON ONLY-')],
108 [sg.Checkbox('Crop image', default=True, key='-CROP-')],
109 [sg.Multiline(size=(63, 10), font=('Courier', 10), key='-ML-')],
110 [sg.Text('Output folder:', size=(15,1)), sg.In(os.path.dirname(__file__), key='-FOLDER-'), sg.FolderBrowse()],
111 [sg.Text('Hardcode filename:', size=(15,1)), sg.In(key='-HARDCODED FILENAME-')],
112 [sg.Button('Refresh'),
113 sg.Button('Snapshot', button_color=('white', 'DarkOrange2')),
114 sg.Exit(button_color=('white', 'sea green'))]]
115
116 window = sg.Window('Window Snapshot', layout, keep_on_top=True, auto_size_buttons=False, default_button_element_size=(12, 1), finalize=True)
117
118 window['-T-'].expand(True, False, False) # causes text to center by expanding the element
119
120 sg.cprint_set_output_destination(window, '-ML-')
121 show_list_by_name(window, '-PROCESSES-', True)
122
123 # ---------------- main loop ----------------
124 while True:
125 # --------- Read and update window --------
126 event, values = window.read()
127 if event in (sg.WIN_CLOSED, 'Exit'):
128 break
129
130 # --------- Do Button Operations --------
131 if event == 'Refresh':
132 show_list_by_name(window, '-PROCESSES-', values['-PYTHON ONLY-'])
133 elif event == 'Snapshot':
134 for i, title in enumerate(values['-PROCESSES-']):
135 sg.cprint('Saving:', end='', c='white on red')
136 sg.cprint(' ', title, colors='white on green')
137 if values['-HARDCODED FILENAME-']:
138 fname = values['-HARDCODED FILENAME-']
139 fname = f'{fname[:-4]}{i}{fname[-4:]}'
140 output_filename = os.path.join(values['-FOLDER-'], fname)
141 else:
142 output_filename = os.path.join(values['-FOLDER-'], f'{title}.png')
143 save_win(output_filename, title, values['-CROP-'])
144 window.close()
145
146
147if __name__ == "__main__":

Callers 1

Calls 5

readMethod · 0.95
closeMethod · 0.95
expandMethod · 0.80
show_list_by_nameFunction · 0.70
save_winFunction · 0.70

Tested by

no test coverage detected