繁体   English   中英

wxPython的New,Save和SaveAs方法

[英]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()的底部注释掉了。还有许多我没有在这里写下来。我的主要问题是如何获得新的filedialog将是您在其中创建文件的保存对话框,而不是打开对话框。

万分感谢。

摘要:

1)如何启动一个允许创建空白文件的FileDialog。 我假设它与保存类似,但是我传递的hwatever ID标志始终为我提供一个“打开”按钮

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

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

以下是在wbPython 2.8.12.1和Python 2.7上的Xubuntu 14.04上为我工作的示例代码:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM