#!/usr/bin/env python
#-*-Coding:utf-8-*-
#=============================================================================
# FileName:
# Desc:
# Author:toughguy
# version:0.0.1
# lastchange:2013-02-20 14:52:11
# History:
#=============================================================================
From Tkinter Import *
Import Tkmessagebox,tkfiledialog
Import Platform
# NL = os.linesep
def openfile ():
Global filename # uses global declaration as global variable to facilitate call by program
Systype = Platform.system () # Judging system type
if Systype = = ' windows ':
Basedir = ' c:\\ '
Else
Basedir = '/'
filename = Tkfiledialog.askopenfilename (initialdir=basedir)
Try
Fobj_r = open (filename, ' R ')
Except IOError, ErrMsg:
print ' * * * Failed Open file: ', errmsg
Else
Editbox.delete (1.0, end)
For Eachline in Fobj_r:
Editbox.insert (INSERT, Eachline)
Fobj_r.close ()
Def savefile ():
Save_data = Editbox.get (1.0, end)
Try
Fobj_w = open (filename, ' W ')
Fobj_w.writelines (Save_data.encode (' Utf-8 '))
Fobj_w.close ()
Tkmessagebox.showinfo (title= ' hint ',
Message= ' Save Success ')
Except IOError, ErrMsg:
Tkmessagebox.showwarning (title= ' save failed ', message= ' Save error ')
Tkmessagebox.showwarning (title= ' error message ', message=errmsg)
Except Nameerror:
Tkmessagebox.showwarning (title= ' save failed ', message= ' not open file ')
Def showlinenum ():
Tkmessagebox.showinfo (title= ' hint ',
Message= ' This feature author does not write now, put it here for decoration. '
def destroy_ui (UI):
Ui.destroy ()
Def aboutauthor ():
AUTHOR_UI = TopLevel ()
Author_ui.title (' about ')
Author_ui.geometry (' 200x80 ')
about_string = Label (author_ui,
text= "Author: Toughguy")
CONFIRMBTN = Button (author_ui, text= ' OK ',
COMMAND=LAMBDA:DESTROY_UI (AUTHOR_UI))
About_string.pack ()
Confirmbtn.pack ()
# Author_ui.mainloop ()
Def createmenus ():
# Initialize Menu
Menubar = Menu (Root)
# Create a File menu
Filemenu = Menu (Menubar, tearoff=0)
Filemenu.add_command (label= ' Open file ', command=openfile)
Filemenu.add_command (label= ' Save file ', Command=savefile)
Filemenu.add_command (label= ' exit ', command=lambda:destroy_ui (root))
Menubar.add_cascade (label= ' files ', menu=filemenu)
# Create Edit Menu
Editmenu = Menu (Menubar, tearoff=0)
Editmenu.add_command (label= ' show line number ', Command=showlinenum)
Menubar.add_cascade (label= ' edit ', Menu=editmenu)
# Create Help Menu
Helpmenu = Menu (Menubar, tearoff=0)
Helpmenu.add_command (label= ' about author ', Command=aboutauthor)
Menubar.add_cascade (label= ' help ', Menu=helpmenu)
Root.config (Menu=menubar)
root = Tk ()
Root.title (' Text editor ')
Root.geometry (' 500x400 ')
Createmenus ()
EditBox = Text (Root, width=70, height=25, bg= ' white ')
Editbox.pack (Side=top, fill=x)
Root.mainloop ()