1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| import Tkinter
def SimpleEditor ( textio ) :
def onSave():
text = self.editor.get( '1.0',Tkinter.END )[:-1]
if self.original != text :
textio.write( text )
self.quit()
def onCopy():
text = self.editor.get( Tkinter.SEL_FIRST,Tkinter.SEL_LAST )
self.clipboard_clear()
self.clipboard_append( text )
def onCut():
text = self.editor.get( Tkinter.SEL_FIRST,Tkinter.SEL_LAST )
self.editor.delete( Tkinter.SEL_FIRST,Tkinter.SEL_LAST )
self.clipboard_clear()
self.clipboard_append( text )
def onPaste():
try : self.editor.insert( Tkinter.INSERT,self.selection_get( selection='CLIPBOARD' ))
except : pass
def selectAll ():
self.editor.tag_remove( Tkinter.SEL,'1.0',Tkinter.END )
self.editor.tag_add( Tkinter.SEL,'1.0',Tkinter.END )
def SetText ():
text = textio.read()
self.original = text[:]
self.editor.focus_force()
self.editor.insert('1.0',unicode( text ))
self.editor.mark_set( Tkinter.INSERT,'1.0' )
font_name,font_size = 'lucida console',10
self = Tkinter.Tk()
self.title( 'simple editor' )
self.protocol("WM_DELETE_WINDOW",self.quit )
menubar = Tkinter.Frame( self )
menubar.pack( fill=Tkinter.X )
Tkinter.Button( menubar,text='Copy' ,command=onCopy ).pack(side=Tkinter.LEFT)
Tkinter.Button( menubar,text='Cut' ,command=onCut ).pack(side=Tkinter.LEFT)
Tkinter.Button( menubar,text='Paste' ,command=onPaste ).pack(side=Tkinter.LEFT)
Tkinter.Button( menubar,text='Quit w/o saving',command=self.quit).pack(side=Tkinter.LEFT)
Tkinter.Button( menubar,text='Save & Quit' ,command=onSave ).pack(expand=Tkinter.YES,fill=Tkinter.BOTH)
self.bind('<Escape>' ,lambda e : onSave())
self.bind('<Control-Enter>',lambda e : onSave())
self.bind('<Control-a>' ,lambda e : selectAll())
self.bind('<Control-A>' ,lambda e : selectAll())
self.editor = Tkinter.Text( self,relief=Tkinter.SUNKEN )
sbar = Tkinter.Scrollbar( self )
sbar.config( command = self.editor.yview )
sbar.pack( side=Tkinter.RIGHT,fill=Tkinter.Y )
self.editor.config( font=(font_name,font_size,'normal' ),width=124,heigh=60,takefocus=True,yscrollcommand=sbar.set )
self.editor.pack( side=Tkinter.LEFT, expand=Tkinter.YES, fill=Tkinter.BOTH )
self.after( 200,SetText )
self.mainloop()
self.destroy()
class FileIO :
def __init__(self,filename) : self.filename = filename
def read (self) : return open( self.filename,'rb' ).read()
def write (self,text) : open( self.filename,'wb' ).write( text )
SimpleEditor( FileIO('test.txt')) |
Partager