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

如何在文件对话框asksaveasfile python中放置默认文件名,python,tkinter,Python,Tkinter,我希望在用户保存文件时,默认情况下在“文件”对话框中放置一个值。谁能告诉我它的语法 saveFilePath = fileDialog.asksaveasfile(mode='w', title="Save the file", defaultextension=".txt") 例如:文件对话框打开时应填写新文件请尝试以下操作: saveFilePath = filedialog.asksaveasfilename(initialdir = "/<file_name>",tit

我希望在用户保存文件时,默认情况下在“文件”对话框中放置一个值。谁能告诉我它的语法

saveFilePath = fileDialog.asksaveasfile(mode='w', title="Save the file", defaultextension=".txt")
例如:文件对话框打开时应填写新文件请尝试以下操作:

saveFilePath  =  filedialog.asksaveasfilename(initialdir = "/<file_name>",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
saveFilePath=filedialog.asksaveasfilename(initialdir=“/”,title=“Select file”,filetypes=((“jpeg文件”、“*.jpg”)、(“所有文件”、“*.jpg”))


正如Bryan Oakley所建议的那样,排序的答案是:intialfile='default\u file\u name'在saveFilePath中。下面是一个Python3块,您可以使用它从CSV读取数据帧,并使用tkinter“另存为”

    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")

initialdir
仅用于目录。对于文件名本身,您需要使用
initialfile
。此外,OP使用了
asksaveasfile
,但您的示例使用了
asksaveasfilename
,这可能会让刚刚开始学习tkinter的人感到困惑。嘿,bryan,您知道一种方法可以做到这一点吗,因为上面的方法只是显示一个搜索结果目录,其中出于某种原因总是搜索登录名
    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")