我有一个保存例程,应该以下列方式提示用户:
- 如果当前选择的文件名存在,提示覆盖
- 如果当前选择的文件名是空的(即“”),设置一个对话框要求用户插入文件名
- 如果当前选择的文件名不存在,请保存!
我的代码目前如下所示,但我觉得应该有更好的方法来做到这一点。就像现在一样,提示用户选择“是,否,取消”的对话框,但我希望它是“是,另存为,取消”。我真的找不到任何方法将“否”按钮更改为“另存为”按钮,该按钮会打开一个对话框,用户可以在其中插入所需的文件名。有什么改进的建议吗?
def saveProject(window):
if os.path.exists(window.getGlobalSettings().getCurrentFileName()): #File exists from before
dlg = wx.MessageDialog(window,
"Overwrite existing project file " + window.getGlobalSettings().getCurrentFileName() + "?",
"Overwrite existing project file",
wx.SAVE|wx.CANCEL|wx.ICON_QUESTION)
result = dlg.ShowModal()
dlg.Destroy()
if result == wx.ID_YES:
save(window,currentFileName)
return True
elif result == wx.ID_SAVEAS:
#TODO: do shit here
return False
elif result == wx.ID_NO:
return False
elif result == wx.ID_CANCEL:
return False
elif window.getGlobalSettings().getCurrentFileName == "":
#TODO: do shit here
return False
else:
save(window,window.getGlobalSettings().getCurrentFileName())
return True
更新
代码成功改成:
def saveProject(window):
dlg = wx.FileDialog(window, "Save project as...", os.getcwd(), "", "*.kfxproject", \
wx.SAVE|wx.OVERWRITE_PROMPT)
result = dlg.ShowModal()
inFile = dlg.GetPath()
dlg.Destroy()
if result == wx.ID_OK: #Save button was pressed
save(window,inFile)
return True
elif result == wx.ID_CANCEL: #Either the cancel button was pressed or the window was closed
return False