wxpython另存为,

时间:2018-11-03 05:36:05

标签: python file wxpython

Example image
我正在尝试解决保存选项,但是我的问题是当我按保存按钮时,如果文件不存在,它将显示一个对话框,要求输入路径/文件名,然后保存文件。

  • 对不起,英语不好,请参见图片。

我希望它的工作方式如下:
    1)打开新文件并写入内容(完成)。
    2)保存“如果是新文件,则必须显示对话框”。
    3)再次按“保存”,如果文件已经存在,则对话框必须消失并且文件必须更新。

感谢和问候,
D. Vinay Singh

def onSaveAs(self, event):
    dlg = wx.FileDialog(self, "Save to file:", ".", "", "Text (*.txt)|*.txt", wx.FD_SAVE)
    if dlg.ShowModal() == wx.ID_OK:
        i = dlg.GetFilterIndex()
        if i == 0: # Text format
            try:
                f = open(dlg.GetPath(), "w")
                print(f)
                hole = self.txt.GetValue()
                print(hole)
                f.write(hole)
            except:
                print("Hello")



def onSave(self, event):
    pathtxt = self.txt_1.GetValue()

    f = open(pathtxt,"w")
    hole_1 = self.txt.GetValue()
    f.write(hole_1)

2 个答案:

答案 0 :(得分:2)

尝试一下:

import os

        def onSave(self, event):
            try:
                f = open(os.path.join(self.dirname, self.filename), 'w')
                f.write(self.control.GetValue())
                f.close()
            except:
                try:
                    dlg = wx.FileDialog(self, "Save to file:", ".", "", "Text (*.txt)|*.txt", wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
                    if (dlg.ShowModal() == wx.ID_OK):
                        self.filename = dlg.GetFilename()
                        self.dirname = dlg.GetDirectory()
                        f = open(os.path.join(self.dirname, self.filename), 'w')
                        f.write(self.control.GetValue())
                        f.close()
                    dlg.Destroy()
                except:
                    pass

        def onSaveAs(self, event):
            try:
                dlg = wx.FileDialog(self, "Save to file:", ".", "", "Text (*.txt)|*.txt", wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
                if (dlg.ShowModal() == wx.ID_OK):
                    self.filename = dlg.GetFilename()
                    self.dirname = dlg.GetDirectory()
                    f = open(os.path.join(self.dirname, self.filename), 'w')
                    f.write(self.control.GetValue())
                    f.close()
                dlg.Destroy()
            except:
                pass

注意:self.filename和self.dirname需要始终启动和跟踪。

答案 1 :(得分:1)

尝试这样的事情:
注意:我尚未测试

def onSave(self, event):
    pathtxt = self.txt_1.GetValue()
    if pathtxt != "":
        if not pathtxt.endswith('.txt'):
            pathtxt=pathtxt+'.txt'

    try:
        with open(pathtxt, 'w') as f:
            f.write(self.txt.GetValue())
    except:
        try:
            dlg = wx.FileDialog(self, "Save to file:", ".", "", "Text (*.txt)|*.txt", wx.FD_SAVE)
            if dlg.ShowModal() == wx.ID_OK:
                i = dlg.GetFilterIndex()
                if i == 0: # Text format
                    try:
                        with open(dlg.GetPath(), 'w') as f:
                            f.write(self.txt.GetValue())
                    except:
                        print("Save failed")
                else:
                    print("Save failed - Use .txt file suffix")
        except:
            print("Save failed - Unknown reason")