Jesvin Jose
Jesvin Jose

Reputation: 23078

Set extension type for open and save dialogs

I want the open dialog to filter files by *.spectrum or not filter it (*.* all files).

I also want the save dialog to suggest a .spectrum extension when saving. The common, new file.ext where the new file is highlighted for us to overwrite.


I have set the wildcard = "*.spectrum" for both options, but please give me a more complete solution.

Upvotes: 1

Views: 1490

Answers (1)

Mike Driscoll
Mike Driscoll

Reputation: 33071

I've written a couple articles on this subject:

Basically what you want for the open and save dialogs is something like this:

wildcard = "Python source (*.spectrum)|*.spectrum|" \
           "All files (*.*)|*.*"

Then in the code, you'd do something like this:

def onOpenFile(self, event):
    """
    Create and show the Open FileDialog
    """
    dlg = wx.FileDialog(
        self, message="Choose a file",
        defaultDir=self.currentDirectory, 
        defaultFile="",
        wildcard=wildcard,
        style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR
        )
    if dlg.ShowModal() == wx.ID_OK:
        paths = dlg.GetPaths()
        print "You chose the following file(s):"
        for path in paths:
            print path
    dlg.Destroy()

#----------------------------------------------------------------------
def onSaveFile(self, event):
    """
    Create and show the Save FileDialog
    """
    dlg = wx.FileDialog(
        self, message="Save file as ...", 
        defaultDir=self.currentDirectory, 
        defaultFile="", wildcard=wildcard, style=wx.SAVE
        )
    if dlg.ShowModal() == wx.ID_OK:
        path = dlg.GetPath()
        print "You chose the following filename: %s" % path
    dlg.Destroy()

Note: Code taken directly from my blog and only modified slightly.

Upvotes: 1

Related Questions