如何在文件对话框中放置默认文件名 asksaveasfile python

Jon*_*Lam 4 python tkinter

当用户保存文件时,我想默认在文件对话框中放置一个值。任何人都可以建议它的语法?

saveFilePath = fileDialog.asksaveasfile(mode='w', title="Save the file", defaultextension=".txt")
Run Code Online (Sandbox Code Playgroud)

例如:打开文件对话框时应填写NewFile

Mac*_*Mac 8

简短的答案是在 saveFilePath 中使用: intialfile = 'default_file_name'。下面是一个 Python 3 块,您可以使用 tkinter 从 CSV 读取数据帧并“另存为”。

    def ExportApplications():
        #reads the file to dataframe
        df_testFile = pd.read_csv('test.csv')

        #creates SaveAs dialogue and prompts user to save
        #you can enter multiple file type formats in data FYI
        data = [('csv', '*.csv')]
        file_out = asksaveasfile(filetypes=data, defaultextension=data,initialfile = "This_is_the_default_file_name_when_saving")

        #writes output to location specified by user in "Save As" dialogue
        df_testFile.to_csv(file_out, index=False, encoding="utf-8")
Run Code Online (Sandbox Code Playgroud)


Jon*_*Lam 7

initialfile
Run Code Online (Sandbox Code Playgroud)

正如布赖恩·奥克利 (Bryan Oakley) 提出的那样奏效。