标签:__init__ com argv nbsp img height tkinter 1.0 ack
1 #coding=utf-8 2 import sys,os 3 import tkFileDialog 4 from Tkinter import * 5 from tkFont import Font 6 7 8 print "欢迎使用编辑器: ", str(sys.argv[0])[:-3] 9 10 class Write(): 11 12 def __init__(self): 13 self.tk=Tk() 14 if len(sys.argv[0])>=2: 15 fname=self.fname=sys.argv[1] 16 else: 17 fname=self.fname=‘new.txt‘ 18 self.createUI(fname) 19 self.tk.mainloop() 20 21 22 def createUI(self,fname): 23 #create menu 24 menubar=Menu(self.tk,bg=‘purple‘) 25 fmenu=Menu(menubar,tearoff=0) 26 fmenu.add_command(label=‘Open‘,command=self.open) 27 fmenu.add_command(label=‘Save‘,command=self.save) 28 fmenu.add_command(label=‘Exit‘,command=exit) 29 menubar.add_cascade(label="File", menu=fmenu) 30 self.tk.title("Welcome to writer!") 31 self.tk.geometry("800x600") 32 self.tk.config(menu=menubar,background="gray",borderwidth = 8) 33 #l = 34 self.txt=Text(width=6000,height=6000,font=Font(self.tk,size=14)) 35 with open(fname) as f: 36 content = f.read() 37 f.close() 38 print content 39 filecontent=content 40 #self.txt.delete(1.0,END) 41 self.txt.insert(INSERT,filecontent) 42 self.txt.pack() 43 44 def save(self): 45 txtContent = self.txt.get(1.0,END) 46 self.saveFile(content=txtContent) 47 48 49 def open(self): 50 self.filename = tkFileDialog.askopenfilename(initialdir = os.getcwd()) 51 filecontent=self.openFile(fname=self.filename) 52 if filecontent is not -1: 53 self.text.delete(1.0,END) 54 self.text.insert(1.0,filecontent) 55 56 def openFile(self,fname=None): 57 if fname is None: 58 return -1 59 self.fname = fname 60 file = open(fname,‘r+‘) 61 content = file.read() 62 file.close() 63 return content 64 65 def saveFile(self,content=None): 66 if content is None: 67 return -1 68 file=open(self.fname,‘w‘) 69 file.write(content) 70 file.flush() 71 file.close() 72 return 0 73 74 def exit(self): 75 sys.exit(0) 76 77 if __name__ == ‘__main__‘: 78 if len(sys.argv)>=2: 79 Write() 80 else: 81 print "请加上需要编辑的文件名,例:python %s new.txt"%sys.argv[0]
标签:__init__ com argv nbsp img height tkinter 1.0 ack
原文地址:http://www.cnblogs.com/cutesnow/p/7009902.html