繁体   English   中英

wxpython另存为,

[英]wxpython save and save as,

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

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

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

谢谢并恭祝安康,
D.维奈·辛格

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)

尝试这个:

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需要始终启动和跟踪。

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

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")

暂无
暂无

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

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