Skip to content Skip to sidebar Skip to footer

How To Use Python Saveas Dialog

I'm trying to find a python function for presenting a 'save file as' dialog that returns a filename as a string. I quickly found the tkFileDialog module, only to realize that its

Solution 1:

Here is a small example for the asksaveasfilename() function. I hope you can use it:

import Tkinter, Tkconstants, tkFileDialog

classTkFileDialogExample(Tkinter.Frame):

    def__init__(self, root):

        Tkinter.Frame.__init__(self, root)
        button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}
        Tkinter.Button(self, text='asksaveasfilename', command=self.asksaveasfilename).pack(**button_opt)

        self.file_opt = options = {}
        options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]
        options['initialfile'] = 'myfile.txt'
        options['parent'] = root

    defasksaveasfilename(self):
        filename = tkFileDialog.asksaveasfilename(**self.file_opt)

        if filename:
            returnopen(filename, 'w')

if __name__=='__main__':
    root = Tkinter.Tk()
    TkFileDialogExample(root).pack()
    root.mainloop()

I was able to open (and create) in-existent files.

Post a Comment for "How To Use Python Saveas Dialog"