python Tkinter tkFileDialog

元气小坏坏 提交于 2021-02-11 12:50:09

问题


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 case tkFileDialog.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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!