diff --git a/docs/sphinx/rest_substitutions/snippets/python/converted/wx.FileDialog.1.py b/docs/sphinx/rest_substitutions/snippets/python/converted/wx.FileDialog.1.py index 50e053b9..b375fede 100644 --- a/docs/sphinx/rest_substitutions/snippets/python/converted/wx.FileDialog.1.py +++ b/docs/sphinx/rest_substitutions/snippets/python/converted/wx.FileDialog.1.py @@ -1,27 +1,23 @@ - + def OnOpen(self, event): - + if self.contentNotSaved: - if wx.MessageBox("Current content has not been saved! Proceed?", "Please confirm", wx.ICON_QUESTION | wx.YES_NO, self) == wx.NO: return - - # else: proceed asking to the user the new file to open - - openFileDialog = wx.FileDialog(self, "Open XYZ file", "", "", - "XYZ files (*.xyz)|*.xyz", wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) - if openFileDialog.ShowModal() == wx.ID_CANCEL: - return # the user changed idea... - - # proceed loading the file chosen by the user - # this can be done with e.g. wxPython input streams: - input_stream = wx.FileInputStream(openFileDialog.GetPath()) - - if not input_stream.IsOk(): - - wx.LogError("Cannot open file '%s'."%openFileDialog.GetPath()) - return - - + # otherwise ask the user what new file to open + with wx.FileDialog(self, "Open XYZ file", wildcard="XYZ files (*.xyz)|*.xyz", + style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog: + + if fileDialog.ShowModal() == wx.ID_CANCEL: + return # the user changed their mind + + # Proceed loading the file chosen by the user + pathname = fileDialog.GetPath() + try: + with open(pathname, 'r') as file: + self.doLoadDataOrWhatever(file) + except IOError: + wx.LogError("Cannot open file '%s'." % newfile) + diff --git a/docs/sphinx/rest_substitutions/snippets/python/converted/wx.FileDialog.2.py b/docs/sphinx/rest_substitutions/snippets/python/converted/wx.FileDialog.2.py index e47a38d6..1ea9c7a2 100644 --- a/docs/sphinx/rest_substitutions/snippets/python/converted/wx.FileDialog.2.py +++ b/docs/sphinx/rest_substitutions/snippets/python/converted/wx.FileDialog.2.py @@ -1,17 +1,17 @@ - + def OnSaveAs(self, event): - - saveFileDialog = wx.FileDialog(self, "Save XYZ file", "", "", - "XYZ files (*.xyz)|*.xyz", wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) - - if saveFileDialog.ShowModal() == wx.ID_CANCEL: - return # the user changed idea... - - # save the current contents in the file - # this can be done with e.g. wxPython output streams: - output_stream = wx.FileOutputStream(saveFileDialog.GetPath()) - - if not output_stream.IsOk(): - wx.LogError("Cannot save current contents in file '%s'."%saveFileDialog.GetPath()) - return - + + with wx.FileDialog(self, "Save XYZ file", wildcard="XYZ files (*.xyz)|*.xyz", + style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog: + + if fileDialog.ShowModal() == wx.ID_CANCEL: + return # the user changed their mind + + # save the current contents in the file + pathname = fileDialog.GetPath() + try: + with open(pathname, 'w') as file: + self.doSaveData(file) + except IOError: + wx.LogError("Cannot save current data in file '%s'." % pathname) +