Make the snippet a little more pythonic. Update the save dialog snippet too.

This commit is contained in:
Robin Dunn
2017-08-04 10:39:19 -07:00
parent 401ac5b614
commit 2e07a611f5
2 changed files with 33 additions and 37 deletions

View File

@@ -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)

View File

@@ -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)