The popup_get_file
Version of Add GUI to Front-End of Script
Why recreate the wheel? There's a Popup
function that will get a Filename for you. This is a single-line GUI:
Shows this window and returns the results from the user interaction with it.
The entire Popup based solution for this get filename example is:
import PySimpleGUI as sg
import sys
if len(sys.argv) == 1:
fname = sg.popup_get_file('Document to open')
else:
fname = sys.argv[1]
if not fname:
sg.popup("Cancel", "No filename supplied")
raise SystemExit("Cancelling: no filename supplied")
else:
sg.popup('The filename you chose was', fname)
How about a GUI and traditional CLI argument in 1 line of code?
import PySimpleGUI as sg
import sys
fname = sys.argv[1] if len(sys.argv) > 1 else sg.popup_get_file('Document to open')
if not fname:
sg.popup("Cancel", "No filename supplied")
raise SystemExit("Cancelling: no filename supplied")
else:
sg.popup('The filename you chose was', fname)