A blog for power systems engineers to learn Python.
Do You Want Files With That? Pop Up Dialog to Ask for PSSE Saved File
Pop up box ask for filename
Today we’ll write an example Python function to ask for filenames. You can use
this function in your Python code to ask for saved case names or for output CSV
file names.
Asking your users for the name of a saved case file using a pop up box is a
nice touch. Though it isn’t the only way to to get filenames. Sometimes people
hard code filenames into the Python script itself:
Here “Darryn” has put the saved case name at the very bottom of the file.
Anyone wanting to run the code on another year’s case or with a different file
version would need to read through the code and find this line to change it.
Another better strategy is to move the filename into its own variable near the
top of the Python script or in a separate file called settings.py where all
configurable aspects of your script should be collected:
topsecret.py
12345678
importpsspy#--------------------------# ConstantsSAVED_CASE="c:/darryn/cases/2001 East Stages v43.sav"#--------------------------
This is better, at least now the configurable portions of our program are in a
predictable location for our colleagues to find. But we can do better, lets
write a function to pop up a box asking the user which file they want to open.
Our pop up box will have a title, “Please select a saved case” and a file type
filter: “Select a .sav file”.
fromtkFileDialogimportaskopenfilename,asksaveasfilenameimportTkinterimporttkFileDialog#------------------------------------------------------------# ExceptionsclassNoFilenameSelected(Exception):"""User didn't select a filename."""#------------------------------------------------------------defask_for_filename(title=None,initialdir=None,filetypes=None,saveas=False):""" Pop up a dialog box asking the user to select a file, Returns the path of the file selected. Args: saveas - If True, use a save as dialog box and allow the user to select a file that does not yet exist. Otherwise the filename must already exist. raises: NoFilenameSelected - If the user presses cancel or 'X' quit on the dialog box. """iftitleisNone:title="Please select a file"ifinitialdirisNone:initialdir="."iffiletypesisNone:filetypes=[('all files','*.*')]# hide the default root Tkinter window.root=Tkinter.Tk()root.withdraw()ifsaveas:askfunction=tkFileDialog.asksaveasfilenameelse:askfunction=tkFileDialog.askopenfilenamefname=askfunction(title=title,initialdir=initialdir,filetypes=filetypes)ifnotfname:raiseNoFilenameSelected("User did not select a filename for '%s'"%title)returnfname#------------------------------------------------------------if__name__=="__main__":fname=ask_for_filename(title="Hi there",filetypes=[("pdf files",".pdf")],saveas=True)printfname
Here are some examples using the new function:
1234567891011
# Only allow selecting .sav files.>>>ask_for_filename("Please select a PSSE Saved case",filetypes=[("Saved Case",".sav")])"C:/darryn/cases/2001 Eastern States High Demand case v43.sav">>># Allow .csv and .txt files>>>ask_for_filename("Please select the output results file",filetypes=[("CSV",".csv"),("Text",".txt")],saveas=True)
Next time you need to ask for a filename why not use the ask_for_filename function?
It’ll pop up and brighten your day.