问题
in short, what's the difference between
tkFileDialog.asksaveasfile
and
tkFileDialog.asksaveasfilename
I could not understand from the build in docs
回答1:
asksaveasfile
asks the user for a file, then opens that file in write mode and returns it to you so you can write in it.
asksaveasfilename
asks the user for a file, then returns that file's name. No file is opened; if you want to write to the file, you'll have to open it yourself.
asksaveasfilename
might be preferred over asksaveasfile
if you want to do something fancier to the file than just writing data to it. For instance, you might want to first copy the file to another directory as a backup. In which case, you'd prefer to get just the file name so you can perform the copy without having to worry about whether having the file open will cause the copy to fail.
回答2:
According to the http://tkinter.unpythonic.net/ wiki:
Similar to:
First you have to decide if you want to open a file or just want to get a filename in order to open the file on your own. In the first case you should use
tkFileDialog.askopenfile()
in the latter casetkFileDialog.askopenfilename()
.
then:
Saving files works in a similar way. You also have two variants of the function, one to get an opened file which you can use to save your data and another to get a file name in order to open the file on your own. These functions are only provided in the single file version. A multiple file version would make no sense.
来源:https://stackoverflow.com/questions/39791942/python-tkinter-tkfiledialog