簡體   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