tkinter 文件对话框中的 asksaveasfile 和 asksaveasfilename 有什么区别?

What is the difference between asksaveasfile and asksaveasfilename in tkinter filedialog?

tkinter filedialog 中的 asksaveasfile 和 asksaveasfilename 有什么区别?

import tkinter as tk
from tkinter import ttk
from tkinter import messagebox as mbox
from tkinter import filedialog as fd
import os
win=tk.Tk()
def save(event=None):
    global url
    try:
        if url:
            content = ide.get(1.0, "end-1c")
            with open(url,"w",encoding="utf-8")as wf:
                wf.write(content)
        else:
            content2 = ide.get(1.0, tk.END)
            url = fd.asksaveasfilename(initialdir=os.getcwd(), defaultextension=".txt", title="Save As",
                                       filetypes=(("Python Files", "*.txt"), ("All Files", "*.*")))
            with open(url,"w")as f:
                f.write(content2)
    except Exception as e:
        return
    except:
        return
text=tk.Text(win,relief="flat",wrap="word")
text.config(font=("Arial",12))
scrollBar=ttk.Scrollbar(win)
scrollBar.pack(side="right",fill="y")
text.pack(expand=True,fill=tk.BOTH)
scrollBar.config(command=text.yview)
text.config(yscrollcommand=scrollBar.set)
text.pack(expand=True,fill=tk.BOTH)
text.focus()
win.mainloop()

来自the docs

asksaveasfile return 一个 file object,而 asksaveasfilename 仅 return 选定的文件名。