1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| import PySimpleGUI as sg import os
def main(): history_file = 'history.txt'
layout = [ [sg.Text('File 1:'), sg.InputText(key='file1'), sg.FileBrowse(initial_folder=get_last_path(history_file, 0))], [sg.Text('File 2:'), sg.InputText(key='file2'), sg.FileBrowse(initial_folder=get_last_path(history_file, 1))], [sg.Text('File 3:'), sg.InputText(key='file3'), sg.FileBrowse(initial_folder=get_last_path(history_file, 2))], [sg.Text('File 4:'), sg.InputText(key='file4'), sg.FileBrowse(initial_folder=get_last_path(history_file, 3))], [sg.Button('OK')], ]
window = sg.Window('File Selector', layout)
while True: event, values = window.read()
if event == sg.WINDOW_CLOSED: break
if event == 'OK': save_history(history_file, values['file1'], values['file2'], values['file3'], values['file4'])
sg.popup('Hello World!') break
window.close()
def get_last_path(history_file, index): if os.path.exists(history_file): with open(history_file, 'r') as file: history = file.readlines() if len(history) > index: return history[index].strip() return ''
def save_history(history_file, *paths): with open(history_file, 'w') as file: for path in paths: file.write(f'{path}\n')
if __name__ == '__main__': main()
|