Skip to main content
 首页 » 编程设计

Python 创建保存按钮,将编辑后的版本保存到同一文件(不是另存为)

2024年11月01日7del

这是我目前正在编写的一个简单的记事本程序。 大多数事情都在工作,但无法让保存工作。 我在定义保存的地方不知道如何创建保存功能(不是另存为)。

不是完整的代码

from tkinter import * 
from tkinter.messagebox import * 
from tkinter.filedialog import * 
from tkinter.font import * 
import sys, time, sched, math 
 
class Format: 
    def __init__(self, notepad): 
        print("Font") 
 
class ZPad: 
    def __init__(self): 
        self.root = Tk() 
        self.root.title("ZPad") 
        self.root.wm_iconbitmap('Notepad.ico') 
 
        self.scrollbar = Scrollbar(self.root) 
        self.scrollbar.pack(side=RIGHT, fill=Y) 
 
        self.textbox = Text(self.root, yscrollcommand=self.scrollbar.set, undo=TRUE) 
        self.textbox.pack(side=LEFT, fill=BOTH, expand=YES) 
 
        #Menu Bar 
        self.menubar = Menu(self.root) 
        self.filemenu = Menu(self.menubar, tearoff=0) 
        self.filemenu.add_command(label="New", command=self.New, accelerator="Ctrl+N") 
        self.filemenu.add_command(label="Open...", command=self.open, accelerator="Ctrl+O") 
        self.filemenu.add_command(label="Save", command=self.Save, accelerator="Ctrl+S") 
        self.filemenu.add_command(label="Save as...", command=self.Save_as, accelerator="Ctrl+Shift+S") 
        self.filemenu.add_separator() 
        self.filemenu.add_command(label="Exit", command=self.quit, accelerator="Ctrl+Q") 
        self.menubar.add_cascade(label="File", menu=self.filemenu) 
 
        self.editmenu = Menu(self.menubar, tearoff=0) 
        self.editmenu.add_command(label="Undo", command=self.Undo, accelerator="Ctrl+Z") 
        self.editmenu.add_command(label="Redo", command=self.Redo, accelerator="Ctrl+Y") 
        self.editmenu.add_separator() 
        self.editmenu.add_command(label="Cut", command=self.Cut, accelerator="Ctrl+X") 
        self.editmenu.add_command(label="Copy", command=self.Copy, accelerator="Ctrl+C") 
        self.editmenu.add_command(label="Paste", command=self.Paste, accelerator="Ctrl+P") 
        self.editmenu.add_command(label="Clear All", command=self.Clear_All, accelerator="Ctrl+Shift+A") 
        self.editmenu.add_separator() 
        self.editmenu.add_command(label="Format", command=self.options, accelerator="Ctrl+T") 
        self.menubar.add_cascade(label="Edit", menu=self.editmenu) 
 
        self.helpmenu = Menu(self.menubar, tearoff=0) 
        self.helpmenu.add_command(label="About...", command=self.About) 
        self.menubar.add_cascade(label="Help", menu=self.helpmenu) 
 
 
        self.root.config(menu=self.menubar) 
 
        self.root.mainloop() 
 
def Save(self): 
    print("Save") 
 
def Save_as(self): 
    global file 
    file = tkinter.filedialog.asksaveasfile(mode='w', defaultextension=".z", filetypes = ( ("ztext file", "*.z"),("zytext", "*.zy") ) ) 
    if file is None: 
        return 
    else: 
        print(file) 
    textoutput = self.textbox.get(0.0, END) 
    file.write(textoutput.rstrip()) 
    file.write("\n") 
 
notepad = ZPad() 

我在 Windows 8.1 中使用 python 3.4。谢谢。

请您参考如下方法:

这是一个保存和另存为函数的简单示例,带有注释:

def save(self): 
    contents = self.textbox.get(1.0,"end-1c") #store the contents of the text widget in a str 
    try:                                       #this try/except block checks to 
        with open(self.f, 'w') as outputFile:  #see if the str containing the output 
            outputFile.write(contents)         #file (self.f) exists, and we can write to it, 
    except AttributeError:                     #and if it doesn't, 
        self.save_as()                         #call save_as 
 
def save_as(self): 
    contents = self.textbox.get(1.0,"end-1c") 
    self.f = tkFileDialog.asksaveasfilename(   #this will make the file path a string 
        defaultextension=".z",                 #so it's easier to check if it exists 
        filetypes = (("ztext file", "*.z"),    #in the save function 
                     ("zytext", "*.zy"))) 
    with open(self.f, 'w') as outputFile: 
        outputFile.write(contents) 

还有其他方法可以做到这一点,但这会在紧要关头起作用。想想 save 函数需要什么:要保存的文本和要保存的文件,然后从那里开始。