1 #Coding=utf-82 ImportSys,os3 ImportTkfiledialog4 fromTkinterImport*5 fromTkfontImportFont6 7 8 Print "You are Welcome to use the Editor:", str (sys.argv[0]) [:-3]9 Ten classWrite (): One A def __init__(self): -self.tk=Tk () - ifLen (sys.argv[0]) >=2: theFname=self.fname=sys.argv[1] - Else: -Fname=self.fname='New.txt' - Self.createui (fname) + Self.tk.mainloop () - + A defCreateui (self,fname): at #Create Menu -Menubar=menu (self.tk,bg='Purple') -Fmenu=menu (menubar,tearoff=0) -Fmenu.add_command (label='Open', command=Self.open) -Fmenu.add_command (label='Save', command=self.save) -Fmenu.add_command (label='Exit', command=exit) inMenubar.add_cascade (label="File", menu=Fmenu) -Self.tk.title ("Welcome to writer!") toSelf.tk.geometry ("800x600") +Self.tk.config (menu=menubar,background="Gray", BorderWidth = 8) - #L = theSelf.txt=text (Width=6000,height=6000,font=font (self.tk,size=14)) * With Open (fname) as F: $Content =F.read ()Panax Notoginseng f.close () - Printcontent theFilecontent=content + #Self.txt.delete (1.0,end) A Self.txt.insert (insert,filecontent) the Self.txt.pack () + - defSave (self): $Txtcontent = Self.txt.get (1.0, END) $Self.savefile (content=txtcontent) - - the defOpen (self): -Self.filename = tkfiledialog.askopenfilename (Initialdir =os.getcwd ())WuyiFilecontent=self.openfile (fname=self.filename) the ifFilecontent is not-1: -Self.text.delete (1.0, END) WuSelf.text.insert (1.0, Filecontent) - About defOpenFile (self,fname=None): $ ifFName isNone: - return-1 -Self.fname =fname -File = Open (fname,'r+') AContent =File.read () + file.close () the returncontent - $ defSaveFile (self,content=None): the ifContent isNone: the return-1 theFile=open (Self.fname,'W') the file.write (content) - File.flush () in file.close () the return0 the About defexit (self): the sys.exit (0) the the if __name__=='__main__': + ifLen (SYS.ARGV) >=2: - Write () the Else:Bayi Print "Please add the name of the file you want to edit, for example: Python%s new.txt"%SYS.ARGV[0]
writer.py
Python based on the TK text editor