X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/299647acac7960652aadb008775429c1f8ea9b8d..6a52ea5cb81dcd07c286d34b80945534780fe05f:/wxPython/demo/FileDialog.py diff --git a/wxPython/demo/FileDialog.py b/wxPython/demo/FileDialog.py index 0d42aa54e6..4c3a664676 100644 --- a/wxPython/demo/FileDialog.py +++ b/wxPython/demo/FileDialog.py @@ -1,7 +1,3 @@ -# 11/17/2003 - Jeff Grimmett (grimmtooth@softhome.net) -# -# o Updated for wx namespace -# import os import wx @@ -9,43 +5,117 @@ import wx #--------------------------------------------------------------------------- # This is how you pre-establish a file filter so that the dialog -# only shows the extention(s) you want it to. -wildcard = "Python source (*.py)|*.py|" \ +# only shows the extension(s) you want it to. +wildcard = "Python source (*.py)|*.py|" \ "Compiled Python (*.pyc)|*.pyc|" \ + "SPAM files (*.spam)|*.spam|" \ + "Egg file (*.egg)|*.egg|" \ "All files (*.*)|*.*" -def runTest(frame, nb, log): - log.WriteText("CWD: %s\n" % os.getcwd()) - - # Create the dialog. In this case the current directory is forced as the starting - # directory for the dialog, and no default file name is forced. This can easilly - # be changed in your program. This is an 'open' dialog, and allows multitple - # file selection to boot. - # - # Finally, of the directory is changed in the process of getting files, this - # dialog is set up to change the current working directory to the path chosen. - dlg = wx.FileDialog( - frame, message="Choose a file", defaultDir=os.getcwd(), - defaultFile="", wildcard=wildcard, style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR - ) - - # Show the dialog and retrieve the user response. If it is the OK response, - # process the data. - if dlg.ShowModal() == wx.ID_OK: - # This returns a Python list of files that were selected. - paths = dlg.GetPaths() +#--------------------------------------------------------------------------- - log.WriteText('You selected %d files:' % len(paths)) +class TestPanel(wx.Panel): + def __init__(self, parent, log): + self.log = log + wx.Panel.__init__(self, parent, -1) + + b = wx.Button(self, -1, "Create and Show an OPEN FileDialog", (50,50)) + self.Bind(wx.EVT_BUTTON, self.OnButton, b) + + b = wx.Button(self, -1, "Create and Show a SAVE FileDialog", (50,90)) + self.Bind(wx.EVT_BUTTON, self.OnButton2, b) + + + def OnButton(self, evt): + self.log.WriteText("CWD: %s\n" % os.getcwd()) + + # Create the dialog. In this case the current directory is forced as the starting + # directory for the dialog, and no default file name is forced. This can easilly + # be changed in your program. This is an 'open' dialog, and allows multitple + # file selections as well. + # + # Finally, if the directory is changed in the process of getting files, this + # dialog is set up to change the current working directory to the path chosen. + dlg = wx.FileDialog( + self, message="Choose a file", defaultDir=os.getcwd(), + defaultFile="", wildcard=wildcard, style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR + ) + + # Show the dialog and retrieve the user response. If it is the OK response, + # process the data. + if dlg.ShowModal() == wx.ID_OK: + # This returns a Python list of files that were selected. + paths = dlg.GetPaths() + + self.log.WriteText('You selected %d files:' % len(paths)) + + for path in paths: + self.log.WriteText(' %s\n' % path) + + # Compare this with the debug above; did we change working dirs? + self.log.WriteText("CWD: %s\n" % os.getcwd()) + + # Destroy the dialog. Don't do this until you are done with it! + # BAD things can happen otherwise! + dlg.Destroy() + + + + def OnButton2(self, evt): + self.log.WriteText("CWD: %s\n" % os.getcwd()) + + # Create the dialog. In this case the current directory is forced as the starting + # directory for the dialog, and no default file name is forced. This can easilly + # be changed in your program. This is an 'save' dialog. + # + # Unlike the 'open dialog' example found elsewhere, this example does NOT + # force the current working directory to change if the user chooses a different + # directory than the one initially set. + dlg = wx.FileDialog( + self, message="Save file as ...", defaultDir=os.getcwd(), + defaultFile="", wildcard=wildcard, style=wx.SAVE + ) + + # This sets the default filter that the user will initially see. Otherwise, + # the first filter in the list will be used by default. + dlg.SetFilterIndex(2) + + # Show the dialog and retrieve the user response. If it is the OK response, + # process the data. + if dlg.ShowModal() == wx.ID_OK: + path = dlg.GetPath() + self.log.WriteText('You selected "%s"' % path) + + # Normally, at this point you would save your data using the file and path + # data that the user provided to you, but since we didn't actually start + # with any data to work with, that would be difficult. + # + # The code to do so would be similar to this, assuming 'data' contains + # the data you want to save: + # + # fp = file(path, 'w') # Create file anew + # fp.write(data) + # fp.close() + # + # You might want to add some error checking :-) + # + + # Note that the current working dir didn't change. This is good since + # that's the way we set it up. + self.log.WriteText("CWD: %s\n" % os.getcwd()) + + # Destroy the dialog. Don't do this until you are done with it! + # BAD things can happen otherwise! + dlg.Destroy() - for path in paths: - log.WriteText(' %s\n' % path) + + +#--------------------------------------------------------------------------- - # Compare this with the debug above; did we change working dirs? - log.WriteText("CWD: %s\n" % os.getcwd()) - # Destroy the dialog. Don't do this until you are done with it! - # BAD things can happen otherwise! - dlg.Destroy() +def runTest(frame, nb, log): + win = TestPanel(nb, log) + return win #--------------------------------------------------------------------------- @@ -66,5 +136,5 @@ filenames combined as a Python list. if __name__ == '__main__': import sys,os import run - run.main(['', os.path.basename(sys.argv[0])]) + run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])