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



当用户保存文件时,我希望默认情况下在文件对话框中放置一个值。有人可以建议它的语法吗?

saveFilePath = fileDialog.asksaveasfile(mode='w', title="Save the file", defaultextension=".txt")

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

initialfile

正如Bryan Oakley提议的那样。

简单的答案是在saveFilePath中使用:intialfile='default_file_name]。下面是Python 3块,您可以使用它从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")

请尝试以下操作:

saveFilePath  =  filedialog.asksaveasfilename(initialdir = "/<file_name>",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))

https://pythonspot.com/tk-file-dialogs/

最新更新