wxPython 新建、保存和另存为方法

wxPython New, Save, and SaveAs Methods

我正在使用 wxPython 为 python 应用编写 UI。我已经处理了一些 OnX 函数,但我需要 OnNew 和 OnSave/SaveAs

方面的帮助

这是我的保存和另存为代码:

def OnSave(self, event):
    self.dirname = ""
    saveFileDialog = wx.FileDialog(self, "Save Operation File", self.dirname, "",
        "Operation Files (*.fwr)|*.fwr|All Files (*.*)|*.*", wx.SAVE|wx.OVERWRITE_PROMPT)
    if saveFileDialog.ShowModal() == wx.ID_OK:
        contents = self.control.GetValue()
        self.filename = saveFileDialog.GetFilename()
        self.dirname = saveFileDialog.GetDirectory()
        filehandle = open(os.path.join(self.dirname, self.filename), 'w')
        filehandle.write(contents)
        filehandle.close()
    else:
        sys.exit(1)
    saveFileDialog.Destroy()

def OnSaveAs(self, event):
    self.dirname = "";

    saveAsFileDialog = wx.FileDialog(self, "Save Operation File As", self.dirname, "",
        "Operation Files (*.fwr)|*.fwr|All Files (*.*)|*.*", 
        wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)

    if saveAsFileDialog.ShowModal() == wx.ID_OK:
        contents = self.control.GetValue()
        self.filename = saveFileDialog.GetFilename()
        self.dirname = saveFileDialog.GetDirectory()
        filehandle = open(os.path.join(self.dirname, self.filename), 'w')
        filehandle.write(contents)
        filehandle.close()
    else:
        sys.exit(1)
    saveFileDialog.Destroy()

    # save current contents in the file
    # use wxPython output streams
    #output_stream = wx.FileOutputStream(saveFileDialog.GetPath())

    #if not output_stream.IsOk():    
    #    wx.LogError("Cannot save contents of Operations File '%s'" % saveFileDialog.GetPath())
    #    return  

底部的注释部分是我发现的另一种方法,使用输入和输出流是否比目前的方法更正确?还有我的另一个问题,我得到了 OnNew Working,这是代码:

def OnNew(self,  event):
    homedir = os.environ['HOME']
    if not os.path.exists(homedir):
        if getpass.getuser():
            homedir = "C:/Users/" + getpass.getuser() + "/"
        else:
            homedir = "C:/"
    newFileDialog = wx.FileDialog(self, "New Operation File", homedir, "",
        "Operation Files (*.fwr)|*.fwr|All Files|(*.*)|*.*", wx.FD_CREATE|wx.OVERWRITE_PROMPT)

一切都很好,但是 OnOpen 方法会打开一个打开的文件对话框,我想要一个创建文件对话框(这与保存相同吗?有人可以给我一个 OnOpen 方法的例子,并让我对我的 OnSave 有一些了解和 OnSaveAs 方法?如您所见,共有三种方法,一种在 OnSaveAs 中,一种在 OnSave 中,另一种在 OnSaveAs() 的底部注释掉。还有很多我没有在这里写下来。不过我的主要问题是如何让 new 的文件对话框成为创建文件的保存对话框,而不是打开的对话框。

非常感谢。

摘要:

1) 如何调出允许创建空白文件的 FileDialog。我想它会类似于保存,但是我传递的 hwatever ID 标志总是给我一个打开按钮

2) 至于保存方法,是按照我在代码中展示的那样更好,还是使用像 SaveAs 中注释掉的部分那样的流更好?

要获取“保存”对话框,您需要将 wx.SAVE 样式标志传递给您的 FileDialog 对象:style=wx.SAVE。您可以阅读有关保存标志的更多信息 here or here.

这是一些示例代码,在 Xubuntu 14.04 上使用 wxPython 2.8.12.1 和 Python 2.7 对我有用:

import os
import wx

wildcard = "Python source (*.py)|*.py|" \
            "All files (*.*)|*.*"

########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY,
                          "File and Folder Dialogs Tutorial")
        panel = wx.Panel(self, wx.ID_ANY)
        self.currentDirectory = os.getcwd()

        saveFileDlgBtn = wx.Button(panel, label="Show SAVE FileDialog")
        saveFileDlgBtn.Bind(wx.EVT_BUTTON, self.onSaveFile)

        # put the buttons in a sizer
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(saveFileDlgBtn, 0, wx.ALL|wx.CENTER, 5)
        panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onSaveFile(self, event):
        """
        Create and show the Save FileDialog
        """
        dlg = wx.FileDialog(
            self, message="Save file as ...", 
            defaultDir=self.currentDirectory, 
            defaultFile="", wildcard=wildcard, style=wx.FD_SAVE
            )
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            print "You chose the following filename: %s" % path
        dlg.Destroy()

#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

我看不出你的储蓄方式有什么问题。在大多数情况下,最好使用 Python 的低级运算符而不是使用 wxPython 的。我会使用 Python 的 with 运算符,因为它更好地遵循较新的习语:

with open(os.path.join(self.dirname, self.filename), 'w') as filehandle:
    filehandle.write(contents)