简体   繁体   English

wxpython另存为,

[英]wxpython save and save as,

Example image 范例图片
I am trying to solve the save option but my problem is when I press save button, if the file does not exist, it should show a dialog box to ask for a path/file name and then save the file. 我正在尝试解决保存选项,但是我的问题是当我按保存按钮时,如果文件不存在,它将显示一个对话框,要求输入路径/文件名,然后保存文件。

  • Sorry for poor English, see the image. 对不起,英语不好,请参见图片。

I want it to work as follows: 我希望它的工作方式如下:
1) Open new file and write content (Done). 1)打开新文件并写入内容(完成)。
2) Save "if it is a new file, the dialog box has to show up". 2)保存“如果是新文件,则必须显示对话框”。
3) again press Save "If the file already exist means the dialog box has to disappear and file has to update. 3)再次按“保存”,如果文件已经存在,则对话框必须消失并且文件必须更新。

Thanks and regards, 谢谢并恭祝安康,
D. Vinay Singh 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)

Try this: 尝试这个:

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

Note: self.filename and self.dirname needs to be initiated and tracked at all times. 注意:self.filename和self.dirname需要始终启动和跟踪。

Try something like this: 尝试这样的事情:
Note: I haven't tested it 注意:我尚未测试

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