A text editor written by Python _python

Source: Internet
Author: User
Tags pack save file

Copy Code code as follows:

#!/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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.