0

我正在用 python 构建一个文本编辑器,比如记事本,我正在使用 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()
4

1 回答 1

3

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

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

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

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

def save(self):
    # filename = tkFileDialog.asksaveasfile(...
    fileName = self.file
于 2012-12-11T15:15:54.783 回答