wxPython New,Save和SaveAs Methods

时间:2015-05-27 13:00:00

标签: python wxpython

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

这是我的Save和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方法打开一个打开的文件对话框,我想要一个创建文件对话框(这和save一样吗?有人可以给我一个示例OnOpen方法,并给我一些洞察我的OnSave和OnSaveAs方法?你可以看到有三个方法,一个在OnSaveAs,一个在OnSave,一个在OnSaveAs()的底部注释。还有更多,我没有在这里写下。我的主要问题虽然是如何让new的filedialog成为一个保存对话框,您可以在其中创建一个文件,而不是一个打开的对话框。

非常感谢。

内容:

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

2)至于保存方法,最好是我在代码中显示的内容,还是使用类似于SaveAs中注释掉的部分的流?。

1 个答案:

答案 0 :(得分:1)

要获取“保存”对话框,您需要将wx.SAVE样式标记传递给FileDialog对象:style=wx.SAVE。您可以详细了解保存标记herehere

这里有一些示例代码,可以在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)