【问题标题】:Implement "Save as" in save dialog wxpython在保存对话框wxpython中实现“另存为”
【发布时间】:2013-01-25 15:05:49
【问题描述】:

我有一个保存例程,它应该以下列方式提示用户:

  • 如果当前选择的文件名存在,提示覆盖
  • 如果当前选择的文件名是空的(即“”),设置一个对话框要求用户插入文件名
  • 如果当前选择的文件名不存在,保存!

我的代码目前如下所示,但我觉得应该有更好的方法来做到这一点。就像现在一样,提示用户选择“是,否,取消”的对话框,但我希望它是“是,另存为,取消”。我真的找不到任何方法将“否”按钮更改为“另存为”按钮,该按钮会打开一个对话框,用户可以在其中插入所需的文件名。有什么改进的建议吗?

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

【问题讨论】:

  • 我不想在python中使用get和set。
  • 从 wxPython 4.1 开始,wx.SAVEwx.OVERWRITE_PROMPT 都有前缀 FD_

标签: python wxpython


【解决方案1】:

您使用了错误的对话框类型。请改用FileDialog

  • 它已经包含wx.FD_OVERWRITE_PROMPT 的“文件是否将被覆盖时提示确认”功能
  • 这是其他人都使用的,所以用户会期待这种对话,当他们得到其他东西时会感到困惑

我找不到在对话框中将“保存”替换为“另存为”的方法(它只有 wx.FD_SAVE),但大多数人不会注意到这一点。

【讨论】:

  • 谢谢,没有注意到那个重要的区别。这样做时,实际上不需要“另存为”对话框,因为文件名总是在打开的浏览器中选择。
猜你喜欢
  • 1970-01-01
  • 2013-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多