如何在打开和更改之后保存以前创建的文本文件?

时间:2012-12-11 15:12:03

标签: python tkinter

我正在python中构建像Notepad这样的文本编辑器,我正在使用Tkinter。 我已成功实现编辑器的新建,打开,另存为,退出,剪切,复制,粘贴,关于和帮助功能。但我正在努力保存功能。 当我打开以前保存的文件并在编辑后保存它的功能就像保存为功能一样。我使用tkFileDialog.asksavefile保存文本文件,使用tkFileDialog.asksavefilename来实现编辑器的另存为功能。

我是python的新手。 谁能在这里指导我?

#! /usr/bin/python
import Tkinter

from Tkinter import *

import tkFileDialog, tkMessageBox     
import os

class OpenEdit:

    def __init__(self):

        # Root Frame widget

        self.root = Tk()

        # Menu panel in frame

        menubar = Menu(self.root)

        # File menu,for open,save,save as and quit       

        filemenu = Menu(menubar)
        editmenu = Menu(menubar)
        filemenu.add_command(label="New", command=self.new)
        filemenu.add_command(label="Open", command=self.open)
        filemenu.add_command(label="Save", command=self.save)
        filemenu.add_command(label="Save as", command=self.save_as)
        filemenu.add_separator()
        filemenu.add_command(label="Quit", command=self.root.quit)
        menubar.add_cascade(label="File", menu=filemenu)

        #Edit menu including Cut, Copy and Paste 
        editmenu = Menu(menubar)
        editmenu.add_command(label="Cut", command=self.cut)
        editmenu.add_command(label="copy", command=self.copy)
        editmenu.add_command(label="Paste", command=self.paste)
        menubar.add_cascade(label="Edit", menu=editmenu)

        # About menu for show about us and help

        aboutmenu = Menu(menubar)
        aboutmenu.add_command(label="About", command=self.about)
        aboutmenu.add_command(label="Help", command=self.help)
        menubar.add_cascade(label="About", menu=aboutmenu)

        # Returning defined setting for GUI

        self.root.config(menu=menubar)





        #Setting up the title of the widget

        self.root.title("Untitled - OpenEdit")

        # Adding Text Widget in the GUI

        self.text = Text(self.root)

        # This line allows it to be resized    

        self.text.pack(expand=YES, fill=BOTH)

        self.root.mainloop()       


        #Defining new method

    def new(self):
        self.root.title("Untitled - OpenEdit")
        self.file = None
        self.text.delete(1.0,END)

    #Defining open method

    def open(self):

        self.file = tkFileDialog.askopenfilename(defaultextension=".txt",filetypes=[("All Files","*.*"),("Text Documents","*.txt")])

        if self.file == "":

            #no file to open

            self.file = None
        else:

            #try to open the file
            #set the window title

            self.root.title(os.path.basename(self.file) + " - OpenEdit")
            self.text.delete(1.0,END)

            file = open(self.file,"r")

            self.text.insert(1.0,file.read())

            file.close()    

    #Defining save method
    def save(self):

        fileName = tkFileDialog.asksaveasfile(mode='w')
        try:
            file = open(fileName, 'w')
            textoutput = self.text.get(0.0, END)
            file.write(textoutput)
        except:
            pass
        finally:
            file.close()

    #Defining save_as method

    def save_as(self):

        fileName = tkFileDialog.asksaveasfilename(initialfile='Untitled.txt',defaultextension=".txt",filetypes=[("All Files","*.*"),("Text Documents","*.txt")])
        try:
            file = open(fileName, 'w')
            textoutput = self.text.get(0.0, END)
            file.write(textoutput)
        except:
            pass
        finally:
            file.close()     

    #Defining cut method

    def cut(self):
        self.text.event_generate("<<Cut>>")

    #Defining copy method

    def copy(self):
        self.text.event_generate("<<Copy>>")

     #Defining paste method
    def paste(self):
        self.text.event_generate("<<Paste>>")

    #Defining about method

    def about(self):
        tkMessageBox.showinfo("OpenEdit","Created by: XYZ")

    #Defining help method

    def help(self):
        tkMessageBox.showinfo("Help","This is help")



#Starting the instance of the class OpenEdit
OpenEdit()

1 个答案:

答案 0 :(得分:3)

您的save函数明确提示输入文件名。如果您不想提示输入文件名,请不要这样做。

相反,请使用当前文件名...我看到open已经保存了它:

def open(self):
    self.file = tkFileDialog.askopenfilename(...

所以你可以在保存中重复使用它:

def save(self):
    # filename = tkFileDialog.asksaveasfile(...
    fileName = self.file