[英]Implement “Save as” in save dialog wxpython
我有一个保存例程,该例程应通过以下方式提示用户:
我的代码当前如下所示,但是我觉得应该有一个更好的方法。 现在,系统会提示用户一个对话框,选择“是,否,取消”,但是我希望它是“是,另存为,取消”。 我真的找不到任何方法可以将“否”按钮更改为“另存为”按钮,以打开一个对话框,用户可以在其中插入所需的文件名。 有任何改善建议吗?
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
UPDATE
该代码已成功更改为:
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
您使用了错误的对话框类型。 改用FileDialog :
wx.FD_OVERWRITE_PROMPT
的“提示确认是否将覆盖文件”功能。 我找不到在对话框中将“保存”替换为“另存为”的方法(它只有wx.FD_SAVE
),但是大多数人不会注意到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.